Hide UISearchBar Cancel Button

ArtSabintsev picture ArtSabintsev · Dec 13, 2011 · Viewed 25.3k times · Source

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?

Answer

Nim Gat picture Nim Gat · Mar 15, 2012

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

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