简体   繁体   English

导航栏IOS 11中的SearchBar

[英]SearchBar in NavigationBar IOS 11

I tried using Navigation Bar Large Title and adding Search Bar to Navigation Controller. 我尝试使用导航栏大标题并将搜索栏添加到导航控制器。 The Search Bar is displayed under the Navigation title. 搜索栏显示在导航标题下。 It should be hidden until I swipe down. 在我向下滑动之前,它应该是隐藏的。 I understand that the default behavior is hidden unless I set "navigationItem.hidesSearchBarWhenScrolling = false". 我了解默认行为是隐藏的,除非我设置为“ navigationItem.hidesSearchBarWhenScrolling = false”。 Even I add "navigationItem.hidesSearchBarWhenScrolling = true", it doesn't work. 即使我添加了“ navigationItem.hidesSearchBarWhenScrolling = true”,它也不起作用。

I created a new app to test this function. 我创建了一个新应用来测试此功能。 It has ViewController and Navigation Controller embedded. 它嵌入了ViewController和Navigation Controller。 Below is my code: 下面是我的代码:

class ViewController: UIViewController {

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.searchController = searchController
        // navigationItem.hidesSearchBarWhenScrolling = true

    }
}

I have encountered with a similar situation. 我遇到过类似情况。 I've solved like this: 我已经这样解决了:

1- Remove everything about searchController 1-删除有关searchController的所有内容

2- Add UISearchBar control into your table 2-将UISearchBar控件添加到表中

在此处输入图片说明

3- Connect this searchBar control to a IBOutlet variable and set delegate 3-将此searchBar控件连接到IBOutlet变量并设置委托

searchBar.delegate = self

4- Use only UISearchBarDelegate (remove UISearchResultsUpdating) and implement textDidChange method 4-仅使用UISearchBarDelegate(删除UISearchResultsUpdating)并实现textDidChange方法

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    filter...
    reload table...
}

this table header search bar works with iOS 11 and older versions 该表格标题搜索栏适用于iOS 11和更早版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM