简体   繁体   中英

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?

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];
}

Try this,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM