Contents

defaultFocus(_:_:priority:)

Defines a region of the window in which default focus is evaluated by assigning a value to a given focus state binding.

Declaration

nonisolated func defaultFocus<V>(_ binding: FocusState<V>.Binding, _ value: V, priority: DefaultFocusEvaluationPriority = .automatic) -> some View where V : Hashable

Parameters

  • binding:

    A focus state binding to update when evaluating default focus in the modified view hierarchy.

  • value:

    The value to set the binding to during evaluation.

  • priority:

    An indication of how to prioritize the preferred default focus target when focus moves into the modified view hierarchy. The default value is automatic, which means the preference will be given priority when focus is being initialized or relocated programmatically, but not when responding to user-directed navigation commands.

Return Value

The modified view.

Discussion

By default, SwiftUI evaluates default focus when the window first appears, and when a focus state binding update moves focus automatically, but not when responding to user-driven navigation commands.

Clients can override the default behavior by specifying an evaluation priority of userInitiated, which causes SwiftUI to use the client’s preferred default focus in response to user-driven focus navigation as well as automatic changes.

In the following example, focus automatically goes to the second of the two text fields when the view is first presented in the window.

WindowGroup {
    VStack {
        TextField(...)
            .focused($focusedField, equals: .firstField)
        TextField(...)
            .focused($focusedField, equals: .secondField)
    }
    .defaultFocus($focusedField, .secondField)
}

See Also

Controlling default focus