隐藏 UISearchBar 取消按钮

发布于 2024-12-20 23:32:40 字数 354 浏览 1 评论 0原文

我有一个 UISearchDisplayController 和 UISearchBar 通过笔尖上的插座连接到我的 ViewController。

我想隐藏取消按钮,以便用户永远看不到它。问题在于,以下代码隐藏了按钮,但仅在向用户显示一毫秒后才隐藏(例如,它在模拟器和设备上闪烁,然后从视图中消失)。

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

有没有更好的方法来隐藏它?

I have a UISearchDisplayController and UISearchBar hooked up to my ViewController via Outlets from my nib.

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 (e.g., 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

苏佲洛 2024-12-27 23:32:41

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

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

On iOS 13.0 and later, UISearchController has this property you can use:

@property (nonatomic) BOOL automaticallyShowsCancelButton API_AVAILABLE(ios(13.0)); // Default YES
森林散布 2024-12-27 23:32:40

我设法通过子类化 UISearchBar 并重写此方法来隐藏“取消”按钮:

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

I managed to hide the "Cancel" button by subclassing UISearchBar and override this method:

-(void)layoutSubviews{
    [super layoutSubviews];
    [self setShowsCancelButton:NO animated:NO];
}
鹿港小镇 2024-12-27 23:32:40

我有同样的问题,但以不同的方式解决了它。

对于那些不能或不想子类化 UISearchDisplayController 的人,我通过在 UIKeyboardWillShowNotification 上添加侦听器并设置 [self setShowsCancelButton:NO 解决了该问题动画:没有] 那里。

viewWillAppear:: 中,

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

然后创建:

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

不要忘记添加,

[[NSNotificationCenter defaultCenter] removeObserver:self];

viewWillDisappear: 中!

希望这有帮助!

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.

In 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:!

Hope this helps!

叹沉浮 2024-12-27 23:32:40

与 Nimrod 的答案类似,您还可以子类化 UISearchDisplayController 并实现 setActive:animated: 方法:

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

Similar to Nimrod's answer, you can also subclass UISearchDisplayController and implement the setActive:animated: method:

- (void)setActive:(BOOL)visible animated:(BOOL)animated {
    [super setActive:visible animated:animated];
    self.searchBar.showsCancelButton = NO;
}
待"谢繁草 2024-12-27 23:32:40

这似乎是 Xcode 中的一个错误。我向苹果的错误报告网站提交了这个错误,他们随后要求提供更多示例代码和用例。

感谢大家尝试解决这个问题。

This seems to be a bug within Xcode. I submitted this error to Apple's bug reporting site, and they've followed up asking for more sample code and use-cases.

Thanks everyone for your attempt at solving this problem.

纵性 2024-12-27 23:32:40
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
        }
    }

}
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
        }
    }

}
白云不回头 2024-12-27 23:32:40

UISearchBarUISearchController 一起使用时遇到此问题。我正在使用自己的取消按钮,因为取消按钮 未在 iPad 上显示showsCancelButton = YES,现在它不会在 iPhone 上隐藏 showsCancelButton = NO

以下内容对我有用。

设置委托和初始值:

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

在文本栏开始编辑后 0.1 秒将 showsCancelButton 重置为 NO

#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;
    });
}

Had this problem when using the UISearchBar with UISearchController. 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!

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.

#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;
    });
}
慕烟庭风 2024-12-27 23:32:40

如果你想避免子类化,请

searchController.searchBar.showsCancelButton = false;

在这两个委托方法中实现 (不要忘记分配委托):

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

- (void)didPresentSearchController:(UISearchController *)searchController

每次更新 searchBar 时都会调用第一个方法(取消按钮默认可见),第二个用于第一次激活 searchBar。

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.

稚然 2024-12-27 23:32:40

只是基于我之前遇到的问题,您是否尝试将其设置为:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller

我不知道如何在您的问题中提出这个问题,如果这是不合适的,抱歉。

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.

℡Ms空城旧梦 2024-12-27 23:32:40

如果在编辑搜索栏的搜索字段时出现取消按钮,您可以执行以下操作;子类化搜索栏并让它实现 UITextFieldDelegate 协议:

@interface CustomAlignedSearchBar : UISearchBar<UITextFieldDelegate>

然后实现 textFieldDidBeginEditing: 并执行以下操作:

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

这将确保取消按钮不会显示。

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 UITextFieldDelegateprotocol:

@interface CustomAlignedSearchBar : UISearchBar<UITextFieldDelegate>

Then implement textFieldDidBeginEditing: and do something like:

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

This will make sure that the cancel button will not show up.

冷…雨湿花 2024-12-27 23:32:40

在 iOS8 中弃用 UISearchDisplayController 之后,Apple 将句柄搜索呈现交给了 UISearchControllerDelegate。

所以你可以重写 searchBar 来隐藏取消按钮,如下所示:

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

如果你需要在非活动状态下隐藏取消按钮,你需要在 init 上设置 searchBar :

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

After UISearchDisplayController deprecated in iOS8, Apple give handle search presentation to UISearchControllerDelegate.

so you can override searchBar to hide the Cancel button, like below :

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

if you need hidden Cancel button from inactive state, you need set searchBar on init :

search = [[UISearchController alloc] initWithSearchResultsController:nil];
[search.searchBar setShowsCancelButton:NO];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文