user187676
user187676

Reputation:

Always show the cancel button in a UISearchBar

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

Answers (3)

Joshua Cancienne
Joshua Cancienne

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

animal_chin
animal_chin

Reputation: 6610

this works

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

Upvotes: 1

Omar Freewan
Omar Freewan

Reputation: 2678

you should use the display delegate

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

Upvotes: 3

Related Questions