7

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.
Community
  • 1
  • 1
banu
  • 787
  • 9
  • 24

5 Answers5

4
UITextField *searchBarTextField = nil;
    for (UIView *subview in self.searchBar.subviews)
    {
        if ([subview isKindOfClass:[UITextField class]])
        {
            searchBarTextField = (UITextField *)subview;
            searchBarTextField.clearButtonMode = UITextFieldViewModeAlways;
            break;
        }
    }
iPatel
  • 46,010
  • 16
  • 115
  • 137
3

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

Sam
  • 7,252
  • 16
  • 46
  • 65
user2526811
  • 1,233
  • 4
  • 20
  • 54
  • 5
    Hi user2526811. i know but my requirement is if text will be blank then if user click the clear button textfield should resign first responder. – banu Aug 13 '13 at 11:52
1

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

enter image description here

And programmatically

myUITextField.clearButtonMode = UITextFieldViewModeAlways;
Sahil Mahajan
  • 3,922
  • 2
  • 29
  • 43
  • ya i set manually in coding, because i am using UISearchbar. @user2526811 says this is default behavior. can i create custom clear button for UITextField..? – banu Aug 13 '13 at 11:47
  • She is asking for the searchBar Not TextField. – user2526811 Aug 13 '13 at 11:47
  • yes if there is nothing in the search bar then it did not show. try to add some character. it is default behaviour – Sahil Mahajan Aug 13 '13 at 11:53
  • 2
    People on this question are having an extremely difficult time with the concept of always. – ed209 May 23 '14 at 14:09
0

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

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
0

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;
}
Yedy
  • 2,107
  • 1
  • 25
  • 30