Always show cancel button in UISearchBar

I use a table view with a UISearchDisplayController in combination with a UISearchBar . The search bar automatically hides the cancel button when it is unacceptable, but I use the view controller in a modal state - so I would like to always show the cancel button and use it to display the modal view controller when the search is canceled.

Is there a way to make the cancel button remain visible without creating a custom search bar?

+8
ios cocoa-touch
source share
3 answers

you must use display delegate

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

it works

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

I found a solution after a lot of searching.

He is currently working for me. After adding the code, I changed the class in Interface Builder to use the class instead of the UISearchBar . I also have a button "Shows the cancel button."

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 
+1
source share

All Articles