简体   繁体   English

隐藏 UISearchBar 取消按钮

[英]Hide UISearchBar Cancel Button

I have a UISearchDisplayController and UISearchBar hooked up to my ViewController via Outlets from my nib.我有一个 UISearchDisplayController 和 UISearchBar 通过我笔尖的 Outlets 连接到我的 ViewController。

I'd like to hide the cancel button so that the user never sees it.我想隐藏取消按钮,以便用户永远不会看到它。 The problem is that the following code hides the button, but only after displaying it to the user for a millisecond (eg, it flashes on the simulator and device and then disappears out of view).问题是下面的代码隐藏了按钮,但只有在向用户显示一毫秒之后(例如,它在模拟器和设备上闪烁,然后消失在视野之外)。

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller 
{
    controller.searchBar.showsCancelButton = NO;
}

Is there a better way to hide it?有没有更好的方法来隐藏它?

I managed to hide the "Cancel" button by subclassing UISearchBar and override this method:我设法通过UISearchBar并覆盖此方法来隐藏“取消”按钮:

-(void)layoutSubviews{
    [super layoutSubviews];
    [self setShowsCancelButton:NO animated:NO];
}

I had the same issue, but fixed it a different way.我有同样的问题,但以不同的方式修复它。

For those who can't or don't want to subclass UISearchDisplayController , I fixed the issue by adding a listener on UIKeyboardWillShowNotification , and setting [self setShowsCancelButton:NO animated:NO] there.对于那些不能或不想UIKeyboardWillShowNotification UISearchDisplayController子类的人,我通过在UIKeyboardWillShowNotification上添加一个侦听器并在UIKeyboardWillShowNotification设置[self setShowsCancelButton:NO animated:NO] UIKeyboardWillShowNotification [self setShowsCancelButton:NO animated:NO]这个问题。

In viewWillAppear: :viewWillAppear:

// Add keyboard observer:
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillAppear:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

Then you create:然后你创建:

- (void)keyboardWillAppear:(NSNotification *)notification
{
    [YOUR-SEARCHBAR-HERE setShowsCancelButton:NO animated:NO];
}

Don't forget to add,别忘了补充,

[[NSNotificationCenter defaultCenter] removeObserver:self];

in viewWillDisappear: !viewWillDisappear:中将viewWillDisappear:

Hope this helps!希望这可以帮助!

Similar to Nimrod's answer, you can also subclass UISearchDisplayController and implement the setActive:animated: method:与 Nimrod 的回答类似,您还可以UISearchDisplayController并实现setActive:animated:方法:

- (void)setActive:(BOOL)visible animated:(BOOL)animated {
    [super setActive:visible animated:animated];
    self.searchBar.showsCancelButton = NO;
}

This seems to be a bug within Xcode.这似乎是 Xcode 中的一个错误。 I submitted this error to Apple's bug reporting site, and they've followed up asking for more sample code and use-cases.我将此错误提交给 Apple 的错误报告站点,他们已跟进要求提供更多示例代码和用例。

Thanks everyone for your attempt at solving this problem.感谢大家尝试解决这个问题。

class CustomSearchBar: UISearchBar {

    override func setShowsCancelButton(showsCancelButton: Bool, animated: Bool) {
        super.setShowsCancelButton(false, animated: false)
    }

}

class CustomSearchController: UISearchController, UISearchBarDelegate {

    lazy var _searchBar: CustomSearchBar = {
        [unowned self] in
        let customSearchBar = CustomSearchBar(frame: CGRectZero)
        customSearchBar.delegate = self
        return customSearchBar
    }()

    override var searchBar: UISearchBar {
        get {
            return _searchBar
        }
    }

}

Had this problem when using the UISearchBar with UISearchController .UISearchBarUISearchController一起使用时遇到了这个问题。 I'm using my own cancel button, as the cancel button wasn't showing on iPad with showsCancelButton = YES , now it won't hide on iPhone with showsCancelButton = NO !我正在使用我自己的取消按钮,因为取消按钮没有在 iPad显示,而且showsCancelButton = YES ,现在它不会在 iPhone 上使用showsCancelButton = NO隐藏!

The following worked for me.以下对我有用。

Set the delegate, and initial value:设置委托和初始值:

- (void)viewDidLoad
{
    // ... 
    self.searchController.searchBar.showsCancelButton = NO;
    self.searchController.searchBar.delegate = self;
}

Reset showsCancelButton to NO 0.1s after the text bar begins editing.showsCancelButton开始编辑后,将showsCancelButton重置为NO 0.1s。

#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        self.searchController.searchBar.showsCancelButton = NO;
    });
}

If you want to avoid the subclassing, implement如果你想避免子类化,实现

searchController.searchBar.showsCancelButton = false;

in these two delegate methods (Do not forget to assign delegates):在这两个委托方法中(不要忘记分配委托):

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

- (void)didPresentSearchController:(UISearchController *)searchController

The first one is called everytime you update the searchBar (Cancel button is visible by default) and the second one is for the first searchBar activation.每次更新搜索栏时都会调用第一个(默认情况下取消按钮可见),第二个用于第一次激活搜索栏。

If the cancel button shows up when editing the search field of the search bar you could do the following;如果在编辑搜索栏的搜索字段时出现取消按钮,您可以执行以下操作; subclass the search bar and have it implement the UITextFieldDelegate protocol:子类化搜索栏并让它实现UITextFieldDelegate协议:

@interface CustomAlignedSearchBar : UISearchBar<UITextFieldDelegate>

Then implement textFieldDidBeginEditing: and do something like:然后实现textFieldDidBeginEditing:并执行以下操作:

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self setShowsCancelButton:self.cancelButtonShown animated:NO];
}

This will make sure that the cancel button will not show up.这将确保取消按钮不会出现。

After UISearchDisplayController deprecated in iOS8, Apple give handle search presentation to UISearchControllerDelegate.在 iOS8 中弃用 UISearchDisplayController 后,Apple 将句柄搜索呈现给 UISearchControllerDelegate。

so you can override searchBar to hide the Cancel button, like below :因此您可以覆盖 searchBar 以隐藏取消按钮,如下所示:

- (void)didPresentSearchController:(UISearchController *)searchController {
    [searchController.searchBar setShowsCancelButton:NO];
}

if you need hidden Cancel button from inactive state, you need set searchBar on init :如果您需要从非活动状态隐藏取消按钮,您需要在 init 上设置 searchBar :

search = [[UISearchController alloc] initWithSearchResultsController:nil];
[search.searchBar setShowsCancelButton:NO];

在 iOS 13.0 及更高版本上, UISearchController具有您可以使用的此属性:

@property (nonatomic) BOOL automaticallyShowsCancelButton API_AVAILABLE(ios(13.0)); // Default YES

Just based on issues I've had before have you tried setting it in:仅基于我之前遇到的问题,您是否尝试将其设置为:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller

I don't know how to ask this question in your question sorry if this is out of place.我不知道如何在你的问题中问这个问题,如果这是不合适的,抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM