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

Swift 4
Make sure you have UISearchBarDelegate defined in your UIViewController

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
}


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...
}