2021-10-14

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

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

[圖片上傳失敗...(image-427f86-1634203890336)]

向日葵的夏天_summer關注

0.216<time datetime="2017-09-28T10:27:54.000Z" style="box-sizing: border-box; margin-right: 10px;">2017.09.28 18:27:54</time>字數 146閱讀 3,724

<article class="_2rhmJa" style="box-sizing: border-box; display: block; font-weight: 400; line-height: 1.8; margin-bottom: 20px; word-break: break-word; color: rgb(64, 64, 64); font-family: -apple-system, system-ui, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

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

}

</article>

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

推薦閱讀更多精彩內容