How it works
FocusInteractions is an option set that declares which kinds of interaction a focusable view supports once it holds focus. Not every focusable view behaves the same way: some merely need to receive focus so they can edit text or scroll, while others act like controls that respond to an activation gesture such as a click, a tap, or pressing the Space bar. By passing a FocusInteractions value to a view's focusable modifier, you tell SwiftUI and the system's accessibility and input layers how the view is meant to be used. Reach for it when a custom view should participate in focus as an actionable control rather than as a passive focus destination.
Choose the interaction with a FocusInteractions option
FocusInteractions exposes static options that name the supported behaviors. The example opts into the activate interaction with
.activate, which marks the view as one the user can trigger like a button — through a click, tap, or the Space bar — rather than one that only accepts focus to begin editing or scrolling.Pass the options to focusable(_:interactions:)
You apply FocusInteractions through the interactions parameter of the focusable modifier. In the example,
.focusable(true, interactions: .activate)makes theText("Activate me")view eligible for focus and specifies that, once focused, it supports activation; the first argument enables focusability and the second supplies the FocusInteractions set.Track the focus state with @FocusState and focused(_:)
A focusable view reports whether it currently holds focus so your UI can react to it. Here
@FocusState private var isFocused: Boolis bound to the view with.focused($isFocused), letting the surrounding code read whether the FocusInteractions-enabled view is the focused element.Reflect focus in the view's appearance
Because focus is observable, you can give the focusable control visible feedback. The example drives
.background(...)and.foregroundStyle(...)offisFocusedso the view highlights inColor.bluewhile focused, and the captionTextbelow it switches between "Focused" and "Not focused" to mirror the same state.
.focusable(true, interactions: .activate) to .focusable(true, interactions: .edit) and observe that the view still takes focus but no longer reports itself as an activatable control.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct FocusInteractionsDemo: View {
@FocusState private var isFocused: Bool
var body: some View {
VStack(spacing: 16) {
Text("Activate me")
.padding()
.background(isFocused ? Color.blue : Color.gray.opacity(0.3))
.foregroundStyle(isFocused ? .white : .primary)
.clipShape(RoundedRectangle(cornerRadius: 8))
.focusable(true, interactions: .activate)
.focused($isFocused)
Text(isFocused ? "Focused" : "Not focused")
.font(.caption)
}
.padding()
}
}