Why do you need to dismiss the keyboard?

While developing a new app, I was creating the standard setup for an account based app, so Login, Register and Forgot Password screens were needed.

After placing the UI Text Fields needed and running the iOS simulator, I entered some text into the first Text Field, however I soon discovered I could not dismiss the keyboard to continue and also noticed the keyboard covered the submit button when it opened. I was stuck! I needed a way to dismiss the keyboard after I entered my text in each Text Field to continue to submit the data.

How To Dismiss the keyboard in a View Controller in Swift 3

There are a few ways to dismiss the keyboard and each has it’s benefits, but the simplest way I have found is, when you add a Tap Gesture to the View controller, so all you have to do is Tap anywhere on the screen to close the keyboard, simple and here is how to do it.

Simply copy the below code snippit and place it after your viewDidLoad() and boom!


//Tap gesture function to close keyboard
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
self.view.endEditing(true)
}