Textfieldのdefault フォーカス設定
- 画面表示時設定
struct ContentView: View {
enum FocusedField {
case firstName, lastName
}
@State private var firstName = ""
@State private var lastName = ""
@FocusState private var focusedField: FocusedField?
var body: some View {
Form {
TextField("First name", text: $firstName)
.focused($focusedField, equals: .firstName)
TextField("Last name", text: $lastName)
.focused($focusedField, equals: .lastName)
}
// または
//.defaultFocus($focusedField, .firstName)
.onAppear {
focusedField = .firstName
}
}
}