1
votes

I know I can set showsCancelButton to NO for a UISearchBar ... until you tap the search bar text field, and then the cancel button appears anyway! (At least it does for me.)

Is there a way to show the "X" in a circle within the UISearchBar text field vs. having that separate Cancel button show up adjacent to it? Note that this button only appears for me when search mode is active, and this is regardless of the setting for showsCancelButton.

4

4 Answers

2
votes

try this

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    // only show the status bar's cancel button while in edit mode
    mySearchBar.showsCancelButton = NO;

}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    mySearchBar.showsCancelButton = NO;
}

when using this cancel button will not shown.

1
votes
for (UIView *searchBarSubview in [searchBar subviews]) {

    if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {

         [(UITextField *)searchBarSubview setClearsOnBeginEditing:YES];
    }
}

to enable the cancelButton try searchBar.showsCancelButton = YES; Try the above lines of code.

0
votes

if you use interface builder it is ery easy to show.it is there by default.i have used search bar in my app and the searchbartextfield has an "X" to clear the textfield

0
votes

It might be that the "X" is only intended to clear the search field and not to cancel the search, and so the Cancel button must remain once a search is in play, regardless of showsCancelButton.