2021-10-14

IOS11--UINavigationItem大标题,搜索栏实现

https://www.jianshu.com/p/7fd93f4e4c17?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

[图片上传失败...(image-427f86-1634203890336)]

向日葵的夏天_summer关注

0.216字数 146阅读 3,724

IOS11--UINavigationItem大标题,搜索栏实现

效果图:

image

实现过程

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!
}

}

你可能感兴趣的:(2021-10-14)