praks5432
praks5432

Reputation: 7792

Cannot stop search bar from showing cancel button

So I'm trying to stop the cancel button from appearing when I click on a search bar. I've tried this -

[self.searchDisplayController.searchBar setShowsCancelButton: NO animated:NO];
NSLog(@"CANCEL BUTTON : %hhd", self.searchDisplayController.searchBar.showsCancelButton);

and the log tells me the showsCancelButton has a value of 0, indicating it's off.

So what's going on?

Upvotes: 0

Views: 294

Answers (2)

Hemant Chittora
Hemant Chittora

Reputation: 3162

Try this,

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
        searchBar.showsCancelButton = NO;
        return YES;
    }

Upvotes: 1

Toseef Khilji
Toseef Khilji

Reputation: 17409

You can simply set the cancel button to hide, in the "searchDisplayControllerWillBeginSearch" delegate method, like:

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
    controller.searchBar.showsCancelButton = NO;
}

and you can also use below if you don't use UISearchDisplayController

-(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    //This'll Show The cancelButton with Animation
    [searchBar setShowsCancelButton:NO animated:YES];
}

Upvotes: 2

Related Questions