Swapnil
Swapnil

Reputation: 1878

clear button in UISearchBar

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

Upvotes: 2

Views: 5210

Answers (2)

PG iOS Developer
PG iOS Developer

Reputation: 41

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;


 }

Upvotes: 1

EmptyStack
EmptyStack

Reputation: 51374

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.

Upvotes: 7

Related Questions