简体   繁体   中英

Tap Gesture Recognizer is inhibiting touch events on UITableViewController

I've set up TapGuestureRecognizer within my ViewDidLoad() to dismiss keyboard. My implementation as follows

class AddRegistrationTableViewController: UITableViewController, UITextFieldDelegate, SelectRoomTableViewControllerDelegate { 
    ...
    override func viewDidLoad() {
    super.viewDidLoad()
    emailAddressTextField.delegate = self
    ...
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:     #selector(dismissKeyboard))
    tableView.addGestureRecognizer(tapGestureRecognizer)
}
...
@objc func dismissKeyboard() {
    emailAddressTextField.endEditing(true)
}

So far so good, this works in dismissing the text field.

But within the Table View Controller, I also have a segue linked up to a cell - which isn't registering the tap (because of the gesture recognizer) to follow through with the segue. I'll need to use 2 fingers to tap on the cell for the segue to be performed. And I've tried removing my above implementation of addGestureRecognizer in the code, and the segue performed as per expectation.

So this led me to believe that the gesture recogniser is inhibiting touch events from registering. Any workarounds or solution that I can implement?

If you want to dismiss the keyboard why aren't you using the UITableViewDelegate method instead of UITapGestureRecognizer .

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      emailAddressTextField.endEditing(true)
}

one more thing, the table view is getting the tap gesture and not cell, thats why the segues are not called.

Instead of tap to dismiss, implement scroll to dismiss by setting the table view's keyboardDismissMode to .onDrag . Have your interface work with the framework, not against it.

A good solution for this would be implementing the UIGestureRecognizerDelegate:

class AddRegistrationTableViewController: UITableViewController, UIGestureRecognizerDelegate, UITextFieldDelegate, SelectRoomTableViewControllerDelegate { 
    ...
    override func viewDidLoad() {
    super.viewDidLoad()
    emailAddressTextField.delegate = self
    ...
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    tapGestureRecognizer.delegate = self // You set the UIGestureRecognizerDelegate here
    tableView.addGestureRecognizer(tapGestureRecognizer)
}
...
@objc func dismissKeyboard() {
    emailAddressTextField.endEditing(true)
}

// In this UIGestureRecognizerDelegate's method we will check if the text field is actually being edited 
// and if it's not the case, we will cancel this touch for the gesture
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return emailAddressTextField.isEditing
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM