UISearchbar keyboard search button Action
In Swift 3:
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
Add UISearchBarDelegate in .h
Also set SearchBar's object delegate to self.
Add this to the UISearchBarDelegate's method:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// Do the search...
}
Swift
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
The textFieldShouldReturn
method is a textField
delegate method and not the one you're looking for. What you need is a UISearchBarDelegate
method called searchButtonClicked
, have a look here.