How to show Xbutton(clear button) always visible in uisearchbar

9.9k Views Asked by At

In my application, I am adding a UISearchBar.

My intent is to enable the UISearch Bar "X button"(clear button in UITextField) to be always visible.

I have tried using the following code below to try to make the "X Button" be always visible. However, it does not work. If I set tf.clearButtonMode = UITextFieldViewModeNever, the clear button in uitextfield not showing. I am not sure what is wrong?

I would really appreciate anyone's help here. Why is this not working?

Code (Not working)

for (UIView* v in searchBar.subviews)
{
    if ( [v isKindOfClass: [UITextField class]] )
    {
        UITextField *tf = (UITextField *)v;
        tf.delegate = self;
        tf.clearButtonMode = UITextFieldViewModeAlways;
        break;
    }
}

Goal:

I want to always show the clear button if the text length is equal to 0

  • i.e. if I don't input any text.
5

There are 5 best solutions below

1
On

I tried to get it but unfortunately , There is no Way of Customising with the ClearButton(X) of UITextField .

There is a way that If You only need it to get resign the KeyBoard , Then just overriding this method :

Just clear the field yourself and call resignFirstResponder .

-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    textField.text = @"";
    [textField resignFirstResponder];

    return NO;
}

Documentation about it HERE

4
On

U can do it in Xib. I am attaching the screenshot.

enter image description here

And programmatically

myUITextField.clearButtonMode = UITextFieldViewModeAlways;
6
On
UITextField *searchBarTextField = nil;
    for (UIView *subview in self.searchBar.subviews)
    {
        if ([subview isKindOfClass:[UITextField class]])
        {
            searchBarTextField = (UITextField *)subview;
            searchBarTextField.clearButtonMode = UITextFieldViewModeAlways;
            break;
        }
    }
1
On

This is the default behavior of the search bar. Because if the UITextField is blank then there is no need to press it.

0
On

This is an older question but I came here with an equal customer request: "Show the clearButton as soon as the cursor is in the searchField. We want to be able to cancel the search with this button in any stage".
I came up with a solution other than adding a custom button:

AppleDocs:

UITextFieldViewModeAlways The overlay view is always displayed if the text field contains text.

So adding a whitespace as the first character will set the clearButton active.
The leading whitespace can be removed as soon as text is entered in the searchField or at any other point before using the text.

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    //adding a whitespace at first start sets the clearButton active
    textField.text = @" ";
}


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    ...
    NSString *completeNewString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    //remove the dummyWhitespace (here or later in code, as needed)
    self.searchString = [completeNewString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    ...
    return YES;
}