Dismiss UISearchBar keyboard without affecting cancel button
I am trying to reproduce the functionality that can be seen in the contacts app on the iphone. I have a UISearchBar that dismisses the keyboard when the search button is clicked. This however deactivitates the cancel button and it requires 2 touches to activate. On the contacts app it is not deactivated when the search button is clicked and the keyboard is dismissed.
So what I am asking is how to dismiss the keyboard without deactivating the cancel button on the uiSearchBar?
I have tried
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
//Some other code
//I have Tried
//Attempt 1
self.searchBar.endEditing(true)
//Attempt 2
self.searchBar.resignFirstResponder()
//Attempt 3
var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar.endEditing(true)
}
Delegate method parses you searchBar , so you do not have to use self.searchBar , that might be one of the issues. I usually use logic from your "Attempt 2".
You can try to implement this:
func searchBarTextDidEndEditing(_ searchBar: UISearchBar)
And call searchBar.resignFirstResponder()
.
If it does not work, then try to implement this method and return true :
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool
If you are using UISearchBar in combination with UISearchDisplatController, then try this method on searchDisplayController :
func setActive(_ visible: Bool, animated animated;: Bool)
尝试,
[self.searchBar resignFirstResponder];
[(UIButton *)[self.searchBar valueForKey:@"_cancelButton"] setEnabled:YES];
链接地址: http://www.djcxy.com/p/7706.html