IOS 11 NavigationBar 中的 SearchBar [英] SearchBar in NavigationBar IOS 11

查看:30
本文介绍了IOS 11 NavigationBar 中的 SearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用导航栏大标题并将搜索栏添加到导航控制器.搜索栏显示在导航标题下.它应该被隐藏,直到我向下滑动.我知道默认行为是隐藏的,除非我设置了navigationItem.hidesSearchBarWhenScrolling = false".即使我添加了navigationItem.hidesSearchBarWhenScrolling = true",它也不起作用.

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". Even I add "navigationItem.hidesSearchBarWhenScrolling = true", it doesn't work.

我创建了一个新应用来测试此功能.它嵌入了 ViewController 和 Navigation Controller.下面是我的代码:

I created a new app to test this function. It has ViewController and Navigation Controller embedded. 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- 删除有关 searchController 的所有内容

1- Remove everything about searchController

2- 将 UISearchBar 控件添加到您的表格中

2- Add UISearchBar control into your table

3- 将此 searchBar 控件连接到 IBOutlet 变量并设置委托

3- Connect this searchBar control to a IBOutlet variable and set delegate

searchBar.delegate = self

4- 仅使用 UISearchBarDelegate(移除 UISearchResultsUpdating)并实现 textDidChange 方法

4- Use only UISearchBarDelegate (remove UISearchResultsUpdating) and implement textDidChange method

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

此表格标题搜索栏适用于 iOS 11 及更早版本

this table header search bar works with iOS 11 and older versions

这篇关于IOS 11 NavigationBar 中的 SearchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆