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

前端 未结 6 779
不知归路
不知归路 2020-12-06 18:33

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

When w

相关标签:
6条回答
  • 2020-12-06 18:54

    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.

    0 讨论(0)
  • 2020-12-06 18:59

    You can try this,

     - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
           if (searchText.length>=2) {
             [Main_SearchBar resignFirstResponder];
    
             // Do your code here
           }
    }
    
    0 讨论(0)
  • 2020-12-06 19:00

    You can try this

    if([self.searchBar.text length] > 2)
    {
        [self.searchBar resignFirstResponder];
    }
    
    0 讨论(0)
  • 2020-12-06 19:06

    Short answer is no...

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

    0 讨论(0)
  • 2020-12-06 19:09

    You can do it by accessing UISearchBar property.

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

    By playing with enablesReturnKeyAutomatically property you can achieve your requirements.

    Thanks.

    0 讨论(0)
  • 2020-12-06 19:13

    This is how i do it:

        if([searchbar.text length] == 0) {
            [searchBar performSelector: @selector(resignFirstResponder)
                               withObject: nil
                               afterDelay: 0.1];
        }
    
    0 讨论(0)
提交回复
热议问题