cgossain cgossain - 1 year ago 60
iOS Question

How to dismiss keyboard when the UISearch Bar 'Search" button is pressed?

I've implemented a UISearch bar with the searching functionality set up, however when I press the "Search" button on the keyboard that shows up nothing happens. How can I get the keyboard to hide when the "Search" button is pressed while keeping the text in the search bar intact (To keep the search results present)?

Answer Source

From Text, Web and Editing Programming Guide for iOS:

To dismiss the keyboard, you call the resignFirstResponder method of the text-based view that is currently the first responder.

So you should do this in your UISearchBarDelegate:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    [searchBar resignFirstResponder];
    // Do the search...