解雇键盘迫使我在tableview中长按单元格

时间:2017-12-16 00:46:32

标签: ios swift uitableview

您好我遇到了一个问题。我的问题如下:

当我使用一个像这样解开键盘的功能时:

//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中点击一个单元格。它只会在我长按时响应。我的tableview在uiviewcontroller中。我将不胜感激任何建议。

1 个答案:

答案 0 :(得分:0)

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

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)
    }
}