Dismiss Keyboard When Tapping Away

Today, we are solving a common issue that many developers face; how to hide the keyboard after it has been used to fill out information. This is vital for your app because if the keyboard doesn’t hide, it leads to bad user experience and no one wants that!

In the view controller you would like to hide the keyboard in, copy and paste the following code:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    return true

This code just detects if the user has tapped the screen and just ends editing. It’s as simple as that. Just copy and paste into any view controller. When the user taps anywhere, the keyboard hides.

Thanks for reading!