Return Value 戻り値
A modified view that sets whether it prefers to be focused by default.
Availability 有効性
Technology
A modified view that sets whether it prefers to be focused by default.
prefersDefaultFocus
A Boolean value that indicates whether this view prefers to receive focus by default. The default value, true
, causes the view to receive focus by default.
namespace
The namespace associated with the focus scope within which this view prefers default focus.
This modifier sets the initial focus preference when no other view has focus. Use the environment value reset
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 prefers
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 Reset
to reevaluate default focus in the main
, which returns the focus to button “3”.
struct ContentView: View {
var mainNamespace
var resetFocus (\.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.