Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing keyboard from UISearchBar when the X button is tapped

I'm using the UISearchBar (but not the SearchDisplayController that's typically used in conjunction) and I'd like to dismiss the keyboard when you hit the 'X' button.

I've followed TomSwift's suggestion on getting called when the 'X' is tapped and that works great. But resigning first responder from the text field and also invoking in the UISearchBar instance, both with resignFirstResponder, won't make the keyboard go away.

Is there a way to get rid of the keyboard when the user has tapped the X button?

Here's what I did to get the 'Clear' notify:

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

Then I have my class setup to implement both UISearchBarDelegate and UITextFieldDelegate.

Having the class serve as the textfield delegate allows me to get this call:

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
     [textField resignFirstResponder];
     [self.searchBar resignFirstResponder];
     return YES;
}

I've tried everything I can think of. The last thing I'm trying is to find a way to issue the 'searchBarCancelButtonClicked' that UISearchDelegate will invoke on my Controller class but not I'm sure how I could do this since the UISearchBar doesn't seem to have any direct methods to invoke with this name.

like image 210
Justin Galzic Avatar asked Nov 16 '10 01:11

Justin Galzic


4 Answers

Toms answer got me thinking. If it is that the search bar is not yet the firstResponder when the user clicks the clear button we can just wait until it is, and then have it resignFirstResponder; i.e. along the lines of:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  [self performFilteringBySearchText: searchText]; // or whatever

  // The user clicked the [X] button or otherwise cleared the text.
  if([searchText length] == 0) {
    [searchBar performSelector: @selector(resignFirstResponder) 
                    withObject: nil 
                    afterDelay: 0.1];
  }
}

Works like a charm, and less hacky than Tom's IMHO.

like image 160
radiospiel Avatar answered Nov 16 '22 02:11

radiospiel


This works:

[searchBar performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.1];
like image 42
pickwick Avatar answered Nov 16 '22 02:11

pickwick


Updated for SWIFT 3:

Suppose that the user has entered a string in searchfield and clicks x the following code works to hide keyboard when x is pressed

`

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) 
{
            if searchBar.text == nil || searchBar.text == ""
            {
                searchBar.perform(#selector(self.resignFirstResponder), with: nil, afterDelay: 0.1)
            }
 }

`

like image 6
Ammad Avatar answered Nov 16 '22 03:11

Ammad


Swift 2 version:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        // The user clicked the [X] button or otherwise cleared the text.
        if (searchText.characters.count == 0) {
            searchBar.performSelector("resignFirstResponder", withObject: nil, afterDelay: 0.1)
        }
    }
like image 3
powertoold Avatar answered Nov 16 '22 03:11

powertoold