Hide UISearchBar Cancel Button

I had the same issue, but fixed it a different way.

For those who can't or don't want to subclass UISearchDisplayController, I fixed the issue by adding a listener on UIKeyboardWillShowNotification, and setting [self setShowsCancelButton:NO animated:NO] there.

In viewWillAppear::

// Add keyboard observer:
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillAppear:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

Then you create:

- (void)keyboardWillAppear:(NSNotification *)notification
{
    [YOUR-SEARCHBAR-HERE setShowsCancelButton:NO animated:NO];
}

Don't forget to add,

[[NSNotificationCenter defaultCenter] removeObserver:self];

in viewWillDisappear:!

Hope this helps!


Similar to Nimrod's answer, you can also subclass UISearchDisplayController and implement the setActive:animated: method:

- (void)setActive:(BOOL)visible animated:(BOOL)animated {
    [super setActive:visible animated:animated];
    self.searchBar.showsCancelButton = NO;
}

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

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