iOS - Dismiss keyboard when touching outside of UITextField


How to Make iOS Keyboard Disappear When You Touch Outside of UITextField 💬💻
Have you ever wondered how to get rid of that pesky iOS keyboard when the user taps anywhere outside of a UITextField? 🤔 You're in luck because I've got the solution for you! 🙌
The Problem 🤔
By default, when a UITextField becomes the first responder and the iOS keyboard appears, it stays visible until the user taps the Return key or another input area in the app. This can be annoying for users who want to dismiss the keyboard quickly and easily by tapping outside of the UITextField. Luckily, there are a few straightforward solutions to address this common issue!
Solution 1: Implementing a Tap Gesture Recognizer 🙌📲
One way to dismiss the keyboard when the user taps outside of a UITextField is by adding a tap gesture recognizer to the parent view of the UITextField. Here's a step-by-step guide on how to implement this solution:
Open your ViewController's .swift file.
Import the UIKit framework.
import UIKit
Declare a tap gesture recognizer as a property in your ViewController.
var tapGesture: UITapGestureRecognizer?
Add the following code to viewDidLoad() or any appropriate method.
tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tapGesture!)
Implement the dismissKeyboard() method that will resign the first responder status of the text field.
@objc func dismissKeyboard() {
view.endEditing(true)
}
Now, whenever the user taps outside of the UITextField, the keyboard will disappear! 🎉
Solution 2: Using the UITextFieldDelegate Protocol 🙌🔣
Another approach is to use the UITextFieldDelegate protocol to detect the tap outside of the UITextField and resign the first responder status. Here's how you can do it:
Conform to the UITextFieldDelegate protocol in your ViewController.
class YourViewController: UIViewController, UITextFieldDelegate {
Set the delegate of the UITextField to your view controller (e.g., inside viewDidLoad()).
yourTextField.delegate = self
Implement the textFieldShouldReturn() method. Inside this method, call the resignFirstResponder() method to dismiss the keyboard.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Implement the touchesBegan() method to detect when the user touches outside of the UITextField and call resignFirstResponder() as well.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
yourTextField.resignFirstResponder()
}
With this approach, the keyboard will disappear when the user taps Return or outside of the UITextField!
Call-to-Action: Share Your Thoughts! 🎉📢
I hope you found these solutions helpful in making the iOS keyboard disappear when the user touches outside of a UITextField. Give them a try and let me know which one works best for you! Do you have any other cool tricks to dismiss the keyboard? Share them in the comments below! 👇
Remember, simplicity is key when it comes to providing the best user experience in your iOS apps. Happy coding! 💻✨
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
