Coding⏱️ 2 min read📅 2026-06-03

How to Fix: How do you dismiss the keyboard when editing a UITextField

How to dismiss keyboard when editing a UITextField in iOS.

Quick Answer: Use the " resignFirstResponder()` method on your UITextField instance, and listen for the "return

To dismiss the keyboard when editing a UITextField, you can use the resignFirstResponder() method. However, as you mentioned, knowing when the user has pressed the 'Done' key on the keyboard requires monitoring specific notifications.

🛠️ Dismissing the Keyboard

  • Call resignFirstResponder() on the UITextField instance to dismiss the keyboard.

To ensure that the keyboard is dismissed after the 'Done' key is pressed, you can use a combination of the resignFirstResponder() method and a notification handler for the UITextField's resignFirstResponder event.

👉 Example Code

Example Code:

textFieldDidEndEditing(_:) { [weak self] textField in
if let weakSelf = self {
weakSelf.view.endEditing(true)
}
}

In this example, the textFieldDidEndEditing(_: _) method is called when the text field's editing has ended. The method then calls the view's endEditing(_:) method to dismiss the keyboard.

✨ Wrapping Up

By using the resignFirstResponder() method and a notification handler, you can ensure that the keyboard is dismissed after the 'Done' key is pressed.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions