ensure keyboard dismiss on view dismiss code example
Example 1: xcode hide keyboard when touch background storyboard
override func viewDidLoad()
{
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing))
view.addGestureRecognizer(tap)
}
Example 2: how to dismiss keyboard in swift
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}