iOS - Dismiss keyboard when touching outside of UITextField

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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:

  1. Open your ViewController's .swift file.

  2. Import the UIKit framework.

import UIKit
  1. Declare a tap gesture recognizer as a property in your ViewController.

var tapGesture: UITapGestureRecognizer?
  1. Add the following code to viewDidLoad() or any appropriate method.

tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tapGesture!)
  1. 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:

  1. Conform to the UITextFieldDelegate protocol in your ViewController.

class YourViewController: UIViewController, UITextFieldDelegate {
  1. Set the delegate of the UITextField to your view controller (e.g., inside viewDidLoad()).

yourTextField.delegate = self
  1. Implement the textFieldShouldReturn() method. Inside this method, call the resignFirstResponder() method to dismiss the keyboard.

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}
  1. 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my