skip to content

In SwiftUI, how do you dismiss the software keyboard from a scrolling checkout form, and what does scrollDismissesKeyboard change?

level: middleimportance: should knowfreq 58%

answer

  1. the keyboard follows focus
  2. nil or false in focus state
  3. four scroll dismissal modes
  4. TextEditor's default differs
  5. number pads have no Return

basics

~10 s

Remove focus: set the @FocusState value to nil or false from onSubmit, a button or a keyboard toolbar item. scrollDismissesKeyboard(_:) (iOS 16) chooses whether scrolling dismisses it: immediately, interactively, never or automatic.

solid answer

~40 s

SwiftUI shows the keyboard while a field has focus, so dismissing it means removing focus. With `@FocusState`, assign `nil` (optional enum) or `false` (Bool); Apple's documentation says that removes focus from the bound fields and dismisses the keyboard. Do it in the last field's `onSubmit`, in the Pay button, and in a `.keyboard` toolbar button, which you need for number and phone pads because on iPhone they have no Return key. `scrollDismissesKeyboard(_:)` (iOS 16) sets how scrollable content treats the keyboard: `.immediately`, `.interactively`, `.never` or `.automatic`. By default a `TextEditor` is interactive and other scrollable content dismisses on scroll, so the modifier mostly picks the style or turns dismissal off.

code

swift · 26 lines
swift
import SwiftUI

struct DeliveryDetailsForm: View {
    enum Field: Hashable { case street, phone }

    @State private var street = ""
    @State private var phone = ""
    @FocusState private var focus: Field?

    var body: some View {
        Form {
            TextField("Street", text: $street)
                .focused($focus, equals: .street)
            TextField("Phone", text: $phone)
                .keyboardType(.phonePad)
                .focused($focus, equals: .phone)
        }
        .scrollDismissesKeyboard(.interactively)
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                Spacer()
                Button("Done") { focus = nil }
            }
        }
    }
}

go deeper

for a junior

Remember that the keyboard follows focus, and that setting the focus state to nil or false hides it.

for a middle

Explain the four ScrollDismissesKeyboardMode values, TextEditor's interactive default, and why a number pad needs a keyboard toolbar button.

for a senior

Choose dismissal behaviour per screen deliberately, and make sure every field whose keyboard lacks a Return key still offers a way to finish editing.

for a principal

Set one keyboard-dismissal convention for every form in the app, so users meet the same Done and scroll behaviour on each screen.

## Why dismissing needs a plan SwiftUI shows the software keyboard while a text field has **focus** and hides it when focus goes away. Rather than a dedicated hide-keyboard call, the routes SwiftUI documents all work by **removing focus**, either because your code clears it or because SwiftUI does so in response to a scroll. A checkout form needs at least two routes, because some keyboards cannot end editing on their own. ## Route 1: clear the focus state If the form tracks focus with **`@FocusState`**, assigning `nil` (for an optional enum) or `false` (for a Bool) removes focus from every bound field. Apple's documentation states this directly: you can remove focus from a `TextField` this way "and thereby dismiss the keyboard". You call it from wherever the intent lives: - the last field's `onSubmit`, after the user presses Done; - a Pay or Save button, before the form is submitted; - a keyboard toolbar button (Route 3). ## Route 2: let scrolling dismiss it **`scrollDismissesKeyboard(_:)`** (iOS 16) configures how scrollable content interacts with the software keyboard. It writes an environment value, so applying it to a `Form`, `List` or `ScrollView` affects the scrollable content inside. The modes are values of `ScrollDismissesKeyboardMode`: | Mode | Behaviour | |---|---| | `.automatic` | The platform default for that kind of scrollable content | | `.immediately` | The keyboard is dismissed as soon as a scroll drag begins | | `.interactively` | The keyboard follows the drag and can be pulled down | | `.never` | Scrolling never dismisses the keyboard | Apple's documentation describes the default: a `TextEditor` is **interactive**, while other kinds of scrollable content **dismiss the keyboard on a scroll** when linked against iOS 16 or later. So you reach for the modifier to choose *how* scrolling dismisses (`.interactively` for a long form people review while typing) or to stop it (`.never`, for a screen where scrolling while typing is normal), not to make dismissal happen at all. ## Route 3: a button on the keyboard On iPhone, `.numberPad` and `.phonePad` keyboards have **no Return key**, so `onSubmit` can never fire from those fields and the user has no key to end editing. A toolbar item with the **`.keyboard`** placement (iOS 15) puts a button above the keyboard; its action clears the focus state. ```swift import SwiftUI struct DeliveryDetailsForm: View { enum Field: Hashable { case street, phone } @State private var street = "" @State private var phone = "" @FocusState private var focus: Field? var body: some View { Form { TextField("Street", text: $street) .focused($focus, equals: .street) TextField("Phone", text: $phone) .keyboardType(.phonePad) .focused($focus, equals: .phone) } .scrollDismissesKeyboard(.interactively) .toolbar { ToolbarItemGroup(placement: .keyboard) { Spacer() Button("Done") { focus = nil } } } } } ``` ## Choosing between them 1. Give every form a **Done path in code**: the last field's `onSubmit` and the primary button both clear focus. 2. Add a **keyboard toolbar** whenever a field uses a keyboard without Return. 3. Decide the **scroll behaviour** deliberately: leave the default, or set `.interactively` or `.never` for the whole form. ## Putting it together in the checkout In the address form, each route has a place. The street and city fields use the text keyboard, so their Return key, titled Next, advances focus in `onSubmit`. The postcode and phone fields use number and phone pads, so a `.keyboard` toolbar offers Next or Done buttons that assign the next case or `nil`. The Pay button clears focus first, so the keyboard is not covering the order summary while payment starts. The whole form sets `.scrollDismissesKeyboard(.interactively)`, so a user who scrolls up to re-check the address can drag the keyboard away without losing what they typed: removing focus never clears a field's text. ## Common mistakes - **Setting focus to a different value and expecting the keyboard to close.** Assigning another field's case moves focus there and keeps the keyboard up; only `nil` or `false` removes it. - **Applying `scrollDismissesKeyboard` to a view that does not scroll**, such as a single field outside any scroll view; nothing scrolls, so nothing is dismissed. - **Forgetting `TextEditor`'s default**: a notes box inside a form keeps the interactive behaviour unless you set a mode for it.

  • Why does assigning another field's case not close the keyboard?
    Writing a `@FocusState` value moves focus to the view marked with that value. Focus is still inside a text field, so the keyboard stays up and simply serves the new field. Only `nil` for an optional enum, or `false` for a Bool, leaves no field focused and lets the keyboard go away.
  • Where do you apply scrollDismissesKeyboard so it covers the whole form?
    On the `Form`, `List` or `ScrollView` itself, or on a container around it. The modifier writes an environment value that scrollable views in that subtree read, so one call at the form level covers its content, including a `TextEditor` inside it.

  • SwiftUI forms never dismiss the keyboard on scroll unless you add scrollDismissesKeyboard
  • Moving focus to another field is a way to close the keyboard
  • onSubmit works on a number pad field because every keyboard has a Return key
  • scrollDismissesKeyboard dismisses the keyboard even on a view that does not scroll
  • A TextEditor dismisses the keyboard immediately on scroll by default