简体   繁体   English

始终在UISearchBar中显示取消按钮

[英]Always show the cancel button in a UISearchBar

I use a table view with a UISearchDisplayController in conjunction with a UISearchBar . 我用表视图与UISearchDisplayController与结合UISearchBar The search bar automatically hides the cancel button when inappropriate, but I'm using the view controller in a modal state - so I would like to always show the cancel button and use it to pop the modal view controller when the search is cancelled. 当不合适时,搜索栏会自动隐藏“取消”按钮,但是我在模式状态下使用视图控制器-因此,我想始终显示取消按钮,并在取消搜索时使用它弹出模式视图控制器。

Is there a way to force the cancel button to stay visible without creating a custom search bar? 有没有一种方法可以强制取消按钮保持可见而无需创建自定义搜索栏?

you should use the display delegate 您应该使用显示委托

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

this works 这有效

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{    
    controller.searchBar.showsCancelButton = YES;
}

I found a solution after searching a lot. 经过大量搜索后,我找到了解决方案

It currently works for me. 目前适用于我。 After adding in the code, I changed the class in Interface Builder to use the class instead of UISearchBar . 添加代码后,我在Interface Builder中将类更改为使用该类而不是UISearchBar I also have "Shows Cancel Button" enabled. 我还启用了“显示取消按钮”。

The code: 代码:

//NoAnimatingSearchBar.h
@interface NoAnimatingSearchBar : UISearchBar

@end

//NoAnimatingSearchBar.m
#import "NoAnimatingSearchBar.h"

@implementation NoAnimatingSearchBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) _destroyCancelButton {
    NSLog(@"_destroyCancelButton");
}

-(void)_setShowsCancelButton:(BOOL)showsCancelButton {
    NSLog(@"_setShowsCancelButton:(BOOL)showsCancelButton");
}
@end

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

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