起動時、TextField にフォーカスを当てる
@FocusState
を使用する
swift
struct ContentView: View {
@FocusState private var isFocused: Bool // ← ★1
var body: some View {
VStack {
TextField("placeholder ..", text: $title)
.focused($isFocused) // ← ★2
.padding()
}.padding()
}
.onAppear() {
isFocused = true // ← ★3
}
}
}
Bool
でなくて enum
でやる
swift
enum FocusInput: Hashable {
case input
case newTag
case fixedTitle
}
struct EventDataInputView: View {
@FocusState var focusInput: FocusInput?
...
focusInput = .input