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,491
  • 16
  • 35
  • 41
hanumanDev
  • 6,378
  • 11
  • 78
  • 144

3 Answers3

3

You can hide your cancel button using this

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}
Rajneesh071
  • 30,084
  • 13
  • 60
  • 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
  • 967
  • 10
  • 24
0

Swift 4.2, 4.0+ of Rajneesh071 answer

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(false, animated: true)
}
Kamran
  • 14,386
  • 3
  • 30
  • 47