UISearchController禁用取消UIBarButtonItem [英] UISearchController disable cancel 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:
< img src =https://i.stack.imgur.com/wBObM.pngalt =在此处输入图像说明>
此取消按钮已消失时间,虽然我正在玩耍,但我现在不能让它出现我的搜索表显示我想要它:
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...
self.resultsViewController = [UITableViewController new];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsViewController];
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = false;
self.searchController.delegate = self;
self.searchBar = self.searchController.searchBar;
self.searchBar.placeholder = self.stage.title;
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.definesPresentationContext = true;
self.navigationItem.titleView = self.searchBar;
self.resultsTableView = self.resultsViewController.tableView;
self.resultsTableView.dataSource = self;
self.resultsTableView.delegate = self;
推荐答案
根据评论更新
UISearchBar
有一个属性(参见 Apple docs )确定是否显示取消按钮:
UISearchBar
has a property (see the Apple docs) which determines whether the cancel button is displayed:
self.searchBar.showsCancelButton = false;
但是,根据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:
@implementation MySearchBar
-(void)setShowsCancelButton:(BOOL)showsCancelButton {
// Do nothing...
}
-(void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated {
// Do nothing....
}
@end
为确保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:
@interface MySearchController () <UISearchBarDelegate> {
UISearchBar *_searchBar;
}
@end
@implementation MySearchController
-(UISearchBar *)searchBar {
if (_searchBar == nil) {
_searchBar = [[MySearchBar alloc] initWithFrame:CGRectZero];
_searchBar.delegate = self;
}
return _searchBar;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchBar.text length] > 0) {
self.active = true;
} else {
self.active = false;
}
}
@end
最后,更改代码实例化这个子类:
Finally, change your code to instantiate this subclass:
self.searchController = [[MySearchController alloc] initWithSearchResultsController:self.resultsViewController];
(您显然需要导入这些子类的相关头文件)。
(You will obviously need to import the relevant header files for these subclasses).
这篇关于UISearchController禁用取消UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!