How to disable / activate the UISearchBar keyboard search button?

I am using UISearchBar in my code. I imported its delegate into the header file and also implemented some delegation methods in the implementation file.

When we click on the UISearchBar , a keyboard will appear to enter text. Keyboard return key - "Search" button. By default, it will turn off. When we enter a character, it will be turned on. (Correctly?)

This is where the problem arises. I want to enable the UISearchBar keyboard return key when the user enters at least two letters.

Is it possible? If so, how can we do this?

thanks

+7
source share
5 answers

You cannot disable the search button. What you can do is use the UISearchBarDelegate methods to find out if you should take action when the search button is clicked, for example:

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

Apple's documentation for this is also very useful and is a great starting point for setting up searchBar behavior.

+7
source

The short answer is no ...

Longer, hacker and more exotic: How to disable / enable return key in UITextField?

+1
source

Here is how I do it:

  if([searchbar.text length] == 0) { [searchBar performSelector: @selector(resignFirstResponder) withObject: nil afterDelay: 0.1]; } 
0
source

You can try this,

  - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{ if (searchText.length>=2) { [Main_SearchBar resignFirstResponder]; // Do your code here } } 
0
source

You can try this

 if([self.searchBar.text length] > 2) { [self.searchBar resignFirstResponder]; } 
-one
source

All Articles