How to Close the iOS Keyboard by Touching Anywhere Using Swift

How to Close the iOS Keyboard by Touching Anywhere Using Swift

6 June 2024 Stephan Petzl Leave a comment Tech-Help

When developing iOS applications, ensuring a smooth user experience is crucial. One common issue developers encounter is how to dismiss the keyboard when the user taps outside of a text field. This guide will walk you through how to implement this functionality using Swift, making your app more user-friendly and intuitive.

Step-by-Step Solution

Below is a simple and effective method to dismiss the keyboard by tapping anywhere on the screen. This approach uses a gesture recognizer to detect taps and then resigns the first responder status of the view, which effectively hides the keyboard.

Implementing the Gesture Recognizer

First, you need to add a tap gesture recognizer to your view. This can be done in the viewDidLoad method of your view controller:

override func viewDidLoad() {
    super.viewDidLoad()

    // Looks for single or multiple taps.
    let tap = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))

    // Uncomment the line below if you want the tap not to interfere and cancel other interactions.
    // tap.cancelsTouchesInView = false 

    view.addGestureRecognizer(tap)
}

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

Using an Extension for Reusability

If you need this functionality across multiple view controllers, you can create an extension for UIViewController:

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

Then, in each view controller where you want this behavior, simply call the hideKeyboardWhenTappedAround method in viewDidLoad:

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

Additional Tips

For a more comprehensive understanding and additional tips on managing the keyboard in iOS development, you can check our detailed articles:

Enhancing Your Development Workflow

Implementing these solutions can significantly improve the user experience of your iOS applications. However, managing multiple functionalities and ensuring everything works seamlessly can be time-consuming. This is where tools like Repeato can be a game-changer.

Repeato is a no-code test automation tool for iOS and Android, designed to help you create, run, and maintain automated tests for your apps efficiently. Its computer vision and AI capabilities make it particularly fast to edit and run tests. With Repeato, you can focus on developing a great product while delegating test automation to non-technical colleagues or QA professionals.

For more information on how Repeato can streamline your development process, check out our documentation or contact us for a demo.

Like this article? there’s more where that came from!