iOS11-設(shè)置導(dǎo)航欄、搜索框和TableView

兩天前在WWDC上蘋果發(fā)布了iOS11 beta1版本,讓我們看下導(dǎo)航欄、搜索框和TableView有什么不一樣的地方。

正常的導(dǎo)航欄

01.jpeg

iOS11的導(dǎo)航欄

viewDidLoad 中加一段代碼

override func viewDidLoad() {
  super.viewDidLoad()
  createData()
        
  // Navigation Bar
  navigationController?.navigationBar.prefersLargeTitles = true
}

就可以得到這樣的效果

向下滑動 Title 變成正常的狀態(tài),向上滑動Title變大
現(xiàn)在點擊一個 cell 進入下一個控制器

在 NavigationController 下的所有控制器的 Title 都改變了
我們可以在一個控制器中單獨設(shè)置 Title 的樣式
在需改修改 Title 樣式的控制器中設(shè)置 navigationItem.largeTitleDisplayMode 屬性來改變 Title 的類型

override func viewDidLoad() {
  super.viewDidLoad()
  // 自動設(shè)置
  // navigationController?.navigationItem.largeTitleDisplayMode = .automatic
  // 大標(biāo)題
  // navigationController?.navigationItem.largeTitleDisplayMode = .always
  // 小標(biāo)題
  navigationController?.navigationItem.largeTitleDisplayMode = .never
}

現(xiàn)在 Title 變成原始狀態(tài)了

Tip:上述的代碼可以在 Xcode9 中的 storyboard 上直接設(shè)置

iOS11的搜索框

添加一個搜索框

override func viewDidLoad() {
  super.viewDidLoad()
  createData()
        
  // Navigation Bar
  navigationController?.navigationBar.prefersLargeTitles = true
  // Search Controller
  let mySearchController: UISearchController = UISearchController(searchResultsController: UIViewController())
  navigationItem.searchController = mySearchController
}

效果圖

可以看到搜索框會下拉出現(xiàn),上拉隱藏,這些系統(tǒng)都幫我們做好了,我們只需要設(shè)置一下就可以有這樣的效果

iOS11的 UITableView

UITableView 多了兩個代理方法

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

先看下效果

我們可以看到 iOS11 中多了從左側(cè)劃出來的 action
大幅度向一個方向滑動會直接觸發(fā) action

具體的實現(xiàn)方法

/// 從左側(cè)劃出
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  let action = UIContextualAction(style: .normal, title: "Mark") { (action, view, handler) in
    self.updateMarkState(for: indexPath)
    handler(true)
  }
  action.backgroundColor = UIColor(colorLiteralRed: 73/255.0, green: 175/255.0, blue: 254/255.0, alpha: 1)
        
  if markState(for: indexPath) {
    action.title = "Unmark"
    action.backgroundColor = UIColor.red
  }
        
  let configuration = UISwipeActionsConfiguration(actions: [action])
  return configuration
}
    
/// 從右側(cè)劃出
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, handler) in
    self.removeItem(at: indexPath)
    handler(true)
  }

  let configuration = UISwipeActionsConfiguration(actions: [action])
  return configuration
}

可以設(shè)置多個action
let configuration = UISwipeActionsConfiguration(actions: [action, ...])


上述的3個特性僅在iOS11上有效

如果要兼容低版本需要判斷當(dāng)前系統(tǒng)的版本

if #available(iOS 11.0, *) {
  navigationController?.navigationBar.prefersLargeTitles = true
} else {
  // Fallback on earlier versions
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容