How to Dismiss the Keyboard When Tapping Outside (Swift)

Sometimes, after entering text into a UITextField, you might want the keyboard to dismiss when you tap anywhere else on the screen.

In this article, I'll explain how to dismiss the keyboard in Swift when tapping outside of a UITextField, as shown in the example below.

Create a Test App

First, let's create a simple test app.

In Xcode, create a new project using [iOS][App].

On the storyboard, the design doesn't need to be perfect. Just add a Text Field and a Button as shown below:

Dismiss the keyboard by tapping outside 1


Next, in ViewController.swift, create a TouchUpInside action for the Button to confirm the event works correctly:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func okButtonTapped(_ sender: Any) {
        print("Tapped!!")
    }
}

How to Dismiss the Keyboard by Tapping Outside

Now let's see how to dismiss the keyboard when tapping anywhere else on the screen in Swift.

There are several ways to dismiss the keyboard, but here we'll use UITapGestureRecognizer.


The endEditing() method on a view checks if there's a text field (or similar) that is the first responder. If so, it automatically calls resignFirstResponder(), which closes the keyboard.

We'll use UITapGestureRecognizer so that when anywhere on the screen is tapped, endEditing() is executed.


Add the following code to ViewController.swift:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let tapGR: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
        tapGR.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tapGR)
    }
    
    @objc func dismissKeyboard() {
        self.view.endEditing(true)
    }
    
    @IBAction func okButtonTapped(_ sender: Any) {
        print("Tapped!!")
    }
}

On line 6, we define a UITapGestureRecognizer so that the dismissKeyboard() function is called when the screen is tapped.

On line 7, cancelsTouchesInView is set to false so that tap-related events continue to work even after the gesture is recognized.

On line 8, we add the gesture recognizer to the ViewController's view. Now, when the screen is tapped, dismissKeyboard() is executed.

In lines 11–13, the dismissKeyboard() function simply calls endEditing() on the ViewController's view. The true argument forces the keyboard to dismiss.


Now, as shown in the demo, the keyboard will dismiss when tapping anywhere outside the UITextField.

When tapping the OK button, the keyboard also dismisses, and the TouchUpInside event runs, printing Tapped!!.

Dismiss the keyboard by tapping outside 2


Implementing as a UIViewController Extension

This dismiss-keyboard behavior is often needed in multiple UIViewControllers that contain input fields.

In that case, it's convenient to implement it as a UIViewController extension.


Create a new Swift file named UIViewController+DismissKeyboard.swift and add the following code:

By convention, extension file names are written as [TypeName]+[ExtensionContent], but it's fine to use a different name.

import UIKit

extension UIViewController {
    
    func setDismissKeyboard() {
        let tapGR: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
        tapGR.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tapGR)
    }
    
    @objc func dismissKeyboard() {
        self.view.endEditing(true)
    }
}

Then, in any ViewController where you want the keyboard to dismiss, just call setDismissKeyboard() in viewDidLoad():

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setDismissKeyboard()
    }

That's it! You now know how to dismiss the keyboard in Swift when tapping anywhere outside of a UITextField.