我的UISearchBar移到顶部(经过动画处理)->想要禁用 [英] My UISearchBar moves to top (animated) --> want to disable

查看:81
本文介绍了我的UISearchBar移到顶部(经过动画处理)->想要禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的tableView上实现一个UISearchBar.

I want implement an UISearchBar on my tableView.

我的代码(在viewDidLoad中):

My code (in viewDidLoad) :

self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.autocapitalizationType = .None
self.searchController.searchBar.autocorrectionType = .No

self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false

self.tableView.tableHeaderView = self.searchController.searchBar

当我点击searchBar时,此举移动到顶部,就像它想要隐藏navigationBar一样:

When I click on the searchBar, this one move to top, like it wants hide the navigationBar:

我在许多帖子中搜索了答案,但没有任何效果.我想禁用此动画,以使searchBar不会移动.

I searched on many posts for an answer but nothing works. I want to disable this animation so that the searchBar doesn't move.

推荐答案

开始搜索时,您可以认为UISearchController是模态显示的.如果您使用常规的UINavigationController设置,此方法会很好用,但是在像您这样的更自定义"的UI中,您可能会遇到诸如搜索控制器附加到错误视图等问题.

You can think of the UISearchController as being presented modally when you start searching. This would work fine if you had a usual UINavigationController setup, however in a more "customized" UI like yours you may run into issues like the search controller attaching to a wrong view, etc.

在您的情况下,我建议不要使用UISearchController,而应使用界面其余部分(可能是在情节提要中?)初始化的单独的UISearchBar,然后手动进行搜索.如果您不能简单地就地过滤内容,请实施UISearchBarDelegate并为搜索结果添加自己的UITableView.

I would suggest not to use UISearchController in your case and use a separate UISearchBar initialised with the rest of your interface (probably in a storyboard?), and then do the search manually. Implement the UISearchBarDelegate and add your own UITableView for the search results, if you can't simply filter your content in place.

尽管看起来您有一个tableView变量-您可以简单地添加另一个属性,类似于用作UITableViewDataSource的属性,然后在其中存储经过过滤的数据.因此,只要在UISearchBar中包含内容,就可以在重新加载表视图数据时简单地使用经过过滤的数据源.例如:

Although looks like you have a tableView var — you could simply add another property, similar to the one you use as a UITableViewDataSource and store filtered data there. So whenever you have something in the UISearchBar you simply use a filtered data source when reloading table view data. For instance:

var data: [String]
var filteredData: [String]

然后在UITableViewDataSource中使用filteredData:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredData.count
}

...

然后在UISearchBarDelegate中执行以下操作:

And then do something like this in the UISearchBarDelegate:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    // Filter the data you have. For instance:
    filteredData = data.filter({$0.rangeOfString(searchText).location != NSNotFound})
    tableView.reloadData()
}

这篇关于我的UISearchBar移到顶部(经过动画处理)->想要禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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