UISearchController 禁用取消 UIBarButtonItem [英] UISearchController disable cancel UIBarButtonItem

查看:27
本文介绍了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...

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屋!

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