objective-cioscocoa-touchuisearchbaruisearchdisplaycontroller

Hide UISearchBar Cancel Button


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?


Solution

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

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