Reputation: 5972
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)?
Upvotes: 27
Views: 29520
Reputation: 452
Swift 4
Make sure you have UISearchBarDelegate defined in your UIViewController
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
Upvotes: 5
Reputation: 386018
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...
}
Upvotes: 56