I have view controller like screenshot below.
When I touch textfield, the keyboard shows up. I want to dismiss keyboard when I tap,drag on any screen.
I tried to put this code in viewcontroller, but doesn't work.
func dismissKey() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer( target: self, action: #selector(dismissKeyboard))
tap.cancelsTouchesInView = false
addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
endEditing(true)
resignFirstResponder()
}
but if I put this code in custom cell class type 1 ,it dismisses keyboard only when I tap on this cell.
how can I dismiss keyboard when I touch on any screen?
If you're trying to dismiss keyboard in UIViewController
try:
@objc func dismissKeyboard() {
view.endEditing(true)
resignFirstResponder()
}