I'm implementing a search controller with a search bar on top and a tableview below which displays the results of the search. I added the following code so that when a user taps on the screen, the keyboard will be dismissed.
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
self.view.addGestureRecognizer(tapGesture)
}
@objc func dismissKeyboard (_ sender: UITapGestureRecognizer) {
if let view = self.view {
view.endEditing(true)
}
}
However, now when I try to implement the didSelectRowAt
method of the table view, it doesn't work as the UITapGestureRecognizer
defined in the code above captures the touch event. Ideally, I'd like the tableview touches to take precedence over the code above, so that when a user touches the tableView, didSelectRowAt
is called, but when he touches anywhere else on screen, dismissKeyboard
is called instead.