将过滤器按钮添加到UISearchController [英] Add filter button to UISearchController

查看:129
本文介绍了将过滤器按钮添加到UISearchController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UISearchController内的searchBar旁边添加一个过滤器"或排序"按钮.我已经尝试将UIButtonsearchbar添加到UIView并将其设置为我的UITableView tableView.tableHeaderView,这不起作用,因为仅在关闭控制器时,tableView.tableHeaderView get设置回了UISearchBar .这是我希望它看起来的示例:

I want to add a "filter" or "sort" button next to the searchBar inside a UISearchController. I have already tried to add the UIButton and the searchbar into a UIView and set this as my UITableView tableView.tableHeaderView This does not work because the tableView.tableHeaderView get's set back to the UISearchBar only when dismissing the controller. Here is an example of how I want it to look:

推荐答案

我可以用几行代码创建一个搜索过滤器.希望对您有帮助.

I could create a search filter with a few lines of code. I hope it helps.

*我选择了书签按钮以应用过滤器图标;因为我已经使用了取消"按钮来执行自定义操作.

*I chose the bookmark button to apply filter icon; because I have already used cancel button for custom action.

首先,您必须委派searchBar来处理书签按钮单击操作. 然后,应按如下所示设置属性.如果要提供自定义位置(默认位置在右侧),则需要setPositionAdjustment.

First, you have to delegate searchBar to handle bookmark button click action. Then you should set properties as below. If you want to give custom position (default position is right side), you need to setPositionAdjustment.

searchController.searchBar.delegate = self
searchController.searchBar.showsBookmarkButton = true
searchController.searchBar.setImage(UIImage(named: "Sort"), for: .bookmark, state: .normal)
// MARK: You may change position of bookmark button.
//searchController.searchBar.setPositionAdjustment(UIOffset(horizontal: 0, vertical: 0), for: .bookmark)

然后,在您的VC中覆盖searchBarBookmarkButtonClicked函数.您可以在此方法内处理click事件.

Then, override searchBarBookmarkButtonClicked function inside your VC. You can handle click event inside this method.

func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) { //showAlert, presentVC or whatever you want here }

func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) { //showAlert, presentVC or whatever you want here }

此外,如果要在搜索控制器处于活动状态时隐藏书签按钮,则需要在updateSearchResults方法内部应用控件.

Also if you want to hide bookmark button while search controller is active, you need to apply control inside updateSearchResults method.

if searchController.isActive {
    searchController.searchBar.showsBookmarkButton = false
} else {
    searchController.searchBar.showsBookmarkButton = true
}


当searchController不活动时:


When searchController is not active:


当searchController处于活动状态时:


When searchController is active:

这篇关于将过滤器按钮添加到UISearchController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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