美文网首页
2021-10-14

2021-10-14

作者: iOSWater | 来源:发表于2021-10-14 17:31 被阅读0次

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

相关文章

  • 2021-10-14 JavaScript事件循环

    title: JavaScript事件循环date: 2021-10-14 11:17:45tags: [Java...

  • 2021-10-14:又见重阳

    2021-10-14:又见重阳 满园花菊郁金黄, 中有孤丛色似霜。 还似今朝歌酒席, ...

  • 亲子阅读篇(二十七) 《乌鸦喝

    绘本名称:《乌鸦喝水》 作者:杨易 ET共读 编绘 阅读时间:2021-10-14 一、亲子阅读 看到书的...

  • 2021-10-15

    2021-10-14,晴转阴,14-22度。 早上空腹血糖,我的6.3,老公的6.6。 午餐吃了胡萝卜羊肉和米饭,...

  • 通过数字确定内容

    2021-10-14 坚持分享第1110天 读书打卡第950天 持续读书打卡第4天,今日读书《超越奇迹》,读书页码...

  • No 221 认认真真做事

    文/Doctorzuo 2021-10-14 这是我重新日更的第221天,小童宝4岁 踏踏实实做人,认认真真做事。...

  • 《金吾生〈正蒙〉日记463.2021-10-14》

    《金吾生〈正蒙〉日记463.2021-10-14》 今天是辛丑戊戌乙未,九月初九,2021-10-14星期四。 【...

  • 2021-10-14

    【2021-10-14】星期四 晴 农历九月初九 伤风,感冒。 今天重阳节,从单位回来匆匆跑去给老爸送点好吃...

  • 宝贝的吐槽

    2021-10-14 周四 多云 一大早,宝贝就开始吐槽。 “我们的班服,本来我们自己都已经选好了颜色和...

  • 反其道而行

    2021-10-14 周四 晴 这周天气状况如过山车般起伏,今天最高气温又回到30度,穿着外套感觉很热。据天气预报...

网友评论

      本文标题:2021-10-14

      本文链接:https://www.haomeiwen.com/subject/fesuoltx.html