Hide the keyBoard when tapping on the View iOS Swift



If the user is started typing in the text field and if the user taps on the view then keyboard wants to be dismissed then as follows.
Code:
class ViewController: UIViewController, UIGestureRecognizerDelegate
{
}


extension ViewController {

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

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift