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

百般思念 提交于 2019-11-28 01:03:19

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.

Alladinian

Short answer is no...

Longer, hackier and more exotic one is here: How to disable/enable the return key in a UITextField?

Dhiraj Umale

This is how i do it:

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

You can try this,

 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
       if (searchText.length>=2) {
         [Main_SearchBar resignFirstResponder];

         // Do your code here
       }
}

You can try this

if([self.searchBar.text length] > 2)
{
    [self.searchBar resignFirstResponder];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!