iOS
keyboard dismissal
iPhone tips
mobile usability
tech guide

How do I dismiss the iOS keyboard?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

On iOS, the keyboard stays visible while a text input remains first responder. So dismissing the keyboard is really about making the current text field or text view resign first responder status. The right pattern depends on whether you want dismissal from the return key, a tap outside the field, or a scrolling gesture.

UIKit: use endEditing(true) for broad dismissal

If you want the whole screen to dismiss whichever input is active, view.endEditing(true) is the most practical approach.

swift
1import UIKit
2
3final class LoginViewController: UIViewController {
4    @IBAction func dismissTapped(_ sender: UIButton) {
5        view.endEditing(true)
6    }
7}

This walks the view hierarchy and tells the active responder to resign. It is simple and works well when several input fields may be on screen.

Dismiss on the return key

For a UITextField, the common pattern is to adopt UITextFieldDelegate and resign first responder inside 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 standard solution when the user is expected to finish editing by pressing Return.

Dismiss when tapping outside the input

For forms, adding a tap gesture recognizer to the background is a common pattern:

swift
1import UIKit
2
3final class LoginViewController: 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}

Setting cancelsTouchesInView to false matters. Without it, the gesture can swallow button taps and make the UI feel broken.

Scroll views can dismiss automatically

If the screen uses a UIScrollView, UITableView, or UICollectionView, you can often get a better experience by dismissing the keyboard during scrolling.

swift
scrollView.keyboardDismissMode = .onDrag

This is especially effective for forms because it feels natural: the user scrolls, and the keyboard gets out of the way.

If you want a softer interaction on supported scroll views, .interactive can make the keyboard track the user's drag instead of disappearing all at once. That often feels better on long forms where the keyboard and content move together.

SwiftUI patterns

In SwiftUI, the modern approach is to use @FocusState when possible.

swift
1import SwiftUI
2
3struct LoginView: View {
4    @FocusState private var focused: Bool
5    @State private var email = ""
6
7    var body: some View {
8        VStack {
9            TextField("Email", text: $email)
10                .focused($focused)
11                .textFieldStyle(.roundedBorder)
12
13            Button("Done") {
14                focused = false
15            }
16        }
17        .padding()
18    }
19}

This is cleaner than sending responder actions through UIApplication because it fits SwiftUI's state-driven model.

You can also pair @FocusState with .onSubmit when pressing Return should close the keyboard immediately:

swift
1TextField("Email", text: $email)
2    .focused($focused)
3    .onSubmit {
4        focused = false
5    }

That keeps the dismissal behavior close to the field itself and works well for login and search forms.

Common Pitfalls

The biggest pitfall is solving only one dismissal path. A screen may need return-key dismissal, outside-tap dismissal, and scroll dismissal together to feel complete.

Another issue is forgetting to wire the delegate for UITextField, which makes textFieldShouldReturn never run.

Gesture recognizers can also interfere with normal interactions if cancelsTouchesInView is left at its default value. That can block buttons or table selection.

Finally, prefer @FocusState in SwiftUI over older UIKit-style hacks when you control the view code. It is easier to reason about and better aligned with modern iOS development.

Summary

  • Dismissing the iOS keyboard means making the active input resign first responder.
  • Use view.endEditing(true) for broad UIKit dismissal.
  • Use textFieldShouldReturn for return-key behavior.
  • Use a background tap gesture or scroll dismissal for form screens.
  • In SwiftUI, prefer @FocusState for keyboard control.

Course illustration
Course illustration

All Rights Reserved.