UITextField
iOS development
Swift programming
keyboard dismissal
app development

How do you dismiss the keyboard when editing a UITextField

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

On iOS, dismissing the keyboard for a UITextField means telling the active control to resign first responder status. The most common solutions are dismissing on the Return key, dismissing when the user taps outside the field, or dismissing the whole view hierarchy with view.endEditing(true).

Dismiss on the Return key

The standard pattern is to make the view controller the text field’s delegate and implement textFieldShouldReturn.

swift
1import UIKit
2
3final class LoginViewController: UIViewController, UITextFieldDelegate {
4    @IBOutlet private weak var emailField: UITextField!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        emailField.delegate = self
9    }
10
11    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
12        textField.resignFirstResponder()
13        return true
14    }
15}

This is the right solution when pressing Return should simply close the keyboard for that field.

Move to the next field in a form

In forms with several inputs, Return often should move focus instead of dismissing immediately.

swift
1import UIKit
2
3final class SignupViewController: UIViewController, UITextFieldDelegate {
4    @IBOutlet private weak var emailField: UITextField!
5    @IBOutlet private weak var passwordField: UITextField!
6
7    override func viewDidLoad() {
8        super.viewDidLoad()
9        emailField.delegate = self
10        passwordField.delegate = self
11    }
12
13    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
14        if textField == emailField {
15            passwordField.becomeFirstResponder()
16        } else {
17            textField.resignFirstResponder()
18        }
19        return true
20    }
21}

This usually feels better than dismissing after every field because it keeps the form flow uninterrupted.

Dismiss when tapping outside

Users also expect the keyboard to disappear when they tap elsewhere on the screen. A tap gesture recognizer is a common solution.

swift
1import UIKit
2
3final class ProfileViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
8        tap.cancelsTouchesInView = false
9        view.addGestureRecognizer(tap)
10    }
11
12    @objc private func dismissKeyboard() {
13        view.endEditing(true)
14    }
15}

view.endEditing(true) asks the entire view hierarchy to make the current first responder resign, so it works even if more text fields are added later.

resignFirstResponder versus endEditing

These two methods solve related problems:

  • 'textField.resignFirstResponder() targets one known text field'
  • 'view.endEditing(true) dismisses whichever editable control is currently active'

If you are in the text field delegate and know exactly which field is active, resignFirstResponder() is fine. If you are handling a background tap, save button, or generic dismissal action, endEditing(true) is often simpler.

Dismiss from a button action

You can also dismiss the keyboard when a button is tapped:

swift
1@IBAction private func saveTapped(_ sender: UIButton) {
2    view.endEditing(true)
3    // Continue with save logic
4}

This is useful when form submission and keyboard dismissal should happen together.

Make the keyboard behavior intentional

Keyboard dismissal is partly a UX choice, not just a code choice. A good form often also sets appropriate return key types:

swift
emailField.returnKeyType = .next
passwordField.returnKeyType = .done

That gives users a clearer signal about whether Return moves forward or finishes editing.

Also remember to keep the active field visible if the keyboard covers it. Dismissing the keyboard is only part of making text entry feel polished.

Common Pitfalls

The biggest mistake is forgetting to set the text field delegate. If the delegate is not assigned, textFieldShouldReturn will never be called.

Another issue is using a tap recognizer that blocks other touch handling. Setting cancelsTouchesInView incorrectly can interfere with buttons, table cells, or collection views.

Developers also call resignFirstResponder() on a specific field even though another field is actually active. In those cases, view.endEditing(true) is more reliable.

Finally, dismissing the keyboard immediately on every Return key can be the wrong UX for multi-field forms. Moving to the next field often feels more natural.

Summary

  • Dismissing the keyboard means making the current first responder resign.
  • 'textFieldShouldReturn is the standard solution for Return-key dismissal.'
  • 'view.endEditing(true) is a convenient generic dismissal method.'
  • Tapping outside the field is commonly handled with a gesture recognizer.
  • In multi-field forms, moving to the next field is often better than dismissing immediately.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.