Answers for "tap on screen to dismiss keyboard swift"

2

xcode hide keyboard when touch background storyboard

override func viewDidLoad()
    {
        super.viewDidLoad()
        
        //Hide Keyboard
        let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing))
        view.addGestureRecognizer(tap)
    }
Posted by: Guest on October-08-2020
0

Close iOS Keyboard by touching anywhere using Swift

// Put this piece of code anywhere you like
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)
    }
}

//Now in every UIViewController, all you have to do is call this function:

override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround() 
}
Posted by: Guest on February-01-2020

Code answers related to "tap on screen to dismiss keyboard swift"

Code answers related to "Swift"

Browse Popular Code Answers by Language