2

Hi can I remove clear button (X button)present in UISearchBar?

I didn't find any delegate for that. There is one for cancel button.

Thanks

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Swapnil
  • 1,858
  • 2
  • 22
  • 49

2 Answers2

7

You can not directly access the clearButton of UISearchBar. You have to loop through the subviews of the UISearchBar to find the UITextField, and set its clearButtonMode property to UITextFieldViewModeNever.

Note: This is not a permanent solution because this may not work in future if the implementation of UISearchBar changes by the subsequent iOS updates.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • for (UIView *subview in searchBar.subviews) { if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) { [(UITextField *)subview setClearButtonMode:UITextFieldViewModeNever]; } } – Swapnil Jul 11 '11 at 06:35
1

You need to get the textField of the Search Bar

UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;

use in - searchBarTextDidBeginEditing method.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
 {

UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;


 }