Reputation:
I use a table view with a UISearchDisplayController
in conjunction with a 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?
Upvotes: 8
Views: 8063
Reputation: 19
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
. 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
Upvotes: 1
Reputation: 6610
this works
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
controller.searchBar.showsCancelButton = YES;
}
Upvotes: 1
Reputation: 2678
you should use the display delegate
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
controller.searchBar.showsCancelButton = YES;
}
Upvotes: 3