您好,我偶然发现了一个问题。我的问题如下:

当我使用这样的功能来关闭键盘时:

//Dismiss keyboard if in keyboard
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(View.dismissKeyboard))
        view.addGestureRecognizer(tap)

@objc func dismissKeyboard() {
    view.endEditing(true)
}


这使我无法在表格视图中点击一个单元格。如果我长按,它只会回应。我的tableview在uiviewcontroller内部。我将不胜感激任何建议。

最佳答案

这对我来说是一种解决方法:

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

10-08 08:05