Reputation: 1673
I have searched items in table view using UISearchBarDelegate. I have set search bar variable's delegate to self like so
searchBar.delegate = self
touchesBegan function I have used to hide keyboard.
override func touchesBegan(_ touches: Set<UITouch>,
with event: UIEvent?) {
self.view.endEditing(true)
}
When i touch any where other than search bar, it doesn't get triggered. Although same thing is working fine with UITextField.
Upvotes: 0
Views: 403
Reputation: 631
(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar This delegate method will help you to perform the above task.
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
return NO; // return NO to not become first responder
}
Upvotes: 0
Reputation: 687
Maybe try:
searchBar.endEditing(true)
or even better:
searchBar.resignFirstResponder()
Edit:
Now that you explained what your issue is, here's what you can do:
func searchBarTextDidBeginEditing(sender: UISearchBar) {
self.tableView.userInteractionEnabled=false
}
And then just reenable on searchBarTextDidEndEditing.
Otherwise what you could do is actually adding the gesture recognizer to the tableView as such:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tableView.addGestureRecognizer(tapGesture)
Upvotes: 1