Prabhakar Patil Prabhakar Patil - 6 months ago 12
Swift Question

Hiding The Keyboard in ios Swift

I want to hide the soft-keyboard whenever the user taps on the screen other than the textfield.

Answer

You can do something like,

override func viewDidLoad() {
super.viewDidLoad()

//Looks for single or multiple taps. 
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}

//Calls this function when the tap is recognized.
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}

Hope this will help :)

Comments