Return Value
戻り値
A modified view that sets whether it prefers to be focused by default.
Discussion
議論
This modifier sets the initial focus preference when no other view has focus. Use the environment value resetFocus
to force a reevaluation of default focus at any time.
The following tvOS example shows three buttons, labeled “1”, “2”, and “3”, in a VStack
. By default, the “1” button would receive focus, because it is the first child in the stack. However, the prefersDefaultFocus(_:in:)
modifier allows button “3” to receive default focus instead. Once the buttons are visible, the user can move down to and focus the “Reset to default focus” button. When the user activates this button, it uses the ResetFocusAction
to reevaluate default focus in the mainNamespace
, which returns the focus to button “3”.
struct ContentView: View {
@Namespace var mainNamespace
@Environment(\.resetFocus) var resetFocus
var body: some View {
VStack {
Button ("1") {}
Button ("2") {}
Button ("3") {}
.prefersDefaultFocus(in: mainNamespace)
Button ("Reset to default focus") {
resetFocus(in: mainNamespace)
}
}
.focusScope(mainNamespace)
}
}
The default focus preference is limited to the focusable ancestor that matches the provided namespace. If multiple views express this preference, then SwiftUI applies the current platform rules to determine which view receives focus.