简体   繁体   中英

Dismiss keyboard forces me to longpress a cell in tableview

Hello I have stumbled over a problem. My problem is as following:

When i use a function that dismisses the keyboard like this:

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

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

It makes me not able to tap a cell in my tableview. It only responds if I longpress. My tableview is inside a uiviewcontroller. I would appreciate any suggestions.

This was a workaround for me:

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

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