IOS11--UINavigationItem大標題,搜索欄實現

IOS11--UINavigationItem大標題,搜索欄實現

效果圖:

IOS11-大標題實現.gif

實現過程

UINavigationItem新增的屬性
  1. largeTitleDisplayMode,控制大標題的顯示,取值:automatic,always,never

    @available(iOS 11.0, *)
    open var largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode
     
     //必須配合使用navigationBar新增屬性 prefersLargeTitles
    @available(iOS 11.0, *)
    open var prefersLargeTitles: Bool
    
  2. searchController,顯示搜索欄,

     @available(iOS 11.0, *)
     open var searchController: UISearchController?  
     
     //滑動時候是否隱藏導航欄上的搜索欄
     @available(iOS 11.0, *)
     open var hidesSearchBarWhenScrolling: Bool
     
     //同時需要設置definesPresentationContext = true,不然進入searchResult控制器時,看不到搜索欄   
    
代碼實現
import UIKit

class ViewController: UIViewController {

var tableView: UITableView!
var searchVC: UISearchController!
var searchResultVC: SearchResultViewController = SearchResultViewController()
var datasArr:[String] = ["1","2","3","4","5","2","3","4","5","2","3","4","5","2","3","4","5"]

override func viewDidLoad() {
    super.viewDidLoad()
    definesPresentationContext = true
    view.backgroundColor = UIColor.white
    self.navigationItem.title = "largeTitle"

    if #available(iOS 11.0, *) {
        self.navigationItem.largeTitleDisplayMode = UINavigationItem.LargeTitleDisplayMode.automatic
    self.navigationController?.navigationBar.prefersLargeTitles = true
        searchVC = UISearchController(searchResultsController: searchResultVC)
        searchVC.searchBar.placeholder = "請輸入搜索內容"
        searchVC.searchResultsUpdater = self
        searchVC.delegate = self
        self.navigationItem.searchController = searchVC
        self.navigationItem.hidesSearchBarWhenScrolling = true
    }
    setupTableView()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
}

func setupTableView() -> Void {
    tableView = UITableView(frame: view.bounds)
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellID")
    tableView.delegate = self
    tableView.dataSource = self
    view.addSubview(tableView)
}
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return datasArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellID")
    cell?.textLabel?.text = datasArr[indexPath.row]
    return cell!
}
}

extension ViewController: UISearchResultsUpdating, UISearchControllerDelegate {
func updateSearchResults(for searchController: UISearchController) {

}
}

class SearchResultViewController: UITableViewController {

var resultTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    
  
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "resultCellID")
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "resultCellID")
    cell?.textLabel?.text = "result\(indexPath.row)"
    return cell!
}

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容