UISearchController disable cancel UIBarButtonItem(UISearchController 禁用取消 UIBarButtonItem)
问题描述
我正在尝试使用 UISearchController 在地图视图上搜索目的地.我希望 UISearchBar 出现在导航栏中,但如果它不显示右侧的取消按钮,我似乎无法这样做:
I am trying to use UISearchController to search for a destination on a map view. I want the UISearchBar to appear in the navigation bar, but I can't seem to make it do so without it showing a cancel button to the right of it:
这个取消"按钮有时会在我玩的时候消失,但我不能让它不出现现在我有搜索表显示我想要它的方式:
This Cancel button has disappeared at times, whilst I'm playing around, but I can't get it to not appear now I have got the search table showing how I want it to:
我敢肯定我做错了什么小事,但我无法弄清楚它是什么......
I'm sure there must be something small I'm doing ever so slightly wrong, but I can't work out what it is...
推荐答案
根据评论更新
UISearchBar
有一个属性(参见 Apple docs) 决定是否显示取消按钮:
UISearchBar
has a property (see the Apple docs) which determines whether the cancel button is displayed:
但是,根据 OP 评论,这不起作用,因为 searchController 不断将取消按钮重新打开.为避免这种情况,请创建 UISearchBar 的子类,并覆盖 setShowsCancelButton
方法:
But, as per OP comments, this does not work, because the searchController keeps switching the cancel button back on. To avoid this, create a subclass of UISearchBar, and override the setShowsCancelButton
methods:
为了确保这个子类被 searchController 使用,我们还需要继承 UISearchController
,并重写 searchBar
方法以返回我们子类的实例.我们还需要确保新的 searchBar 激活了 searchController - 我选择使用 UISearchBarDelegate
方法 textDidChange
来实现这一点:
To ensure this subclass is used by the searchController, we also need to subclass UISearchController
, and override the searchBar
method to return an instance of our subclass. We also need to ensure that the new searchBar activates the searchController - I've chosen to use the UISearchBarDelegate
method textDidChange
for this:
最后,修改你的代码来实例化这个子类:
Finally, change your code to instantiate this subclass:
(您显然需要为这些子类导入相关的头文件).
(You will obviously need to import the relevant header files for these subclasses).
这篇关于UISearchController 禁用取消 UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!