将过滤器按钮添加到UISearchController

编程入门 行业动态 更新时间:2024-10-28 04:30:14
本文介绍了将过滤器按钮添加到UISearchController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在UISearchController内的searchBar旁边添加一个过滤器"或排序"按钮.我已经尝试将UIButton和searchbar添加到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

本文发布于:2023-11-09 17:22:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   按钮   UISearchController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!