How can I disable/enable UISearchBar keyboard's Search button? How can I disable/enable UISearchBar keyboard's Search button? objective-c objective-c

How can I disable/enable UISearchBar keyboard's Search button?


You can't disable the search button. What you can do is use the UISearchBarDelegate methods to figure out if you should take action on the search button being clicked, like so:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {    if (searchBar.text.length < 2) {        return;    }    else {        // Do search stuff here    }}

The Apple Documentation for this is very useful as well, and is a great starting point for customizing the searchBar's behavior.


You can do it by accessing UISearchBar property.

let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextFieldtextFieldInsideSearchBar?.enablesReturnKeyAutomatically = false

By playing with enablesReturnKeyAutomatically property you can achieve your requirements.

Thanks.