How to override the cancel button in the UISearchB

2019-02-25 16:29发布

问题:

I am currently using the following method to stop the cancel button item from showing up in the search bar. I have a custom UIButton that I would like to use instead.

The problem is that at the moment the built in cancel button is still showing up.

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

thanks for any help

回答1:

You can hide your cancel button using this

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}


回答2:

for (UIView *possibleButton in searchBar.subviews)
{  
    if ([possibleButton isKindOfClass:[UIButton class]])  
    {  
        UIButton *cancelButton = (UIButton*)possibleButton;  
        cancelButton.enabled = YES;  
        break;
    }    
}

one go through this link UISearchBar disable auto disable of cancel button



回答3:

Swift 4.2, 4.0+ of Rajneesh071 answer

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(false, animated: true)
}