IOS11--UINavigationItem大标题,搜索栏实现
[图片上传失败...(image-427f86-1634203890336)]
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大标题,搜索栏实现
效果图:
实现过程
UINavigationItem新增的属性
-
largeTitleDisplayMode,控制大标题的显示,取值:automatic,always,never
@available(iOS 11.0, *) open var largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode //必须配合使用navigationBar新增属性 prefersLargeTitles @available(iOS 11.0, *) open var prefersLargeTitles: Bool
-
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>