3

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

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
hanumanDev
  • 6,592
  • 11
  • 82
  • 146

3 Answers3

3

You can hide your cancel button using this

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0
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

Community
  • 1
  • 1
ganesh manoj
  • 977
  • 10
  • 24
0

Swift 4.2, 4.0+ of Rajneesh071 answer

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(false, animated: true)
}
Kamran
  • 14,987
  • 4
  • 33
  • 51