How can I focus on a specific text field when the view appears? I have a basic form where I want the title field is be focused when the modal displays. I have the following code which achieves this but there has to be a cleaner/better way. Can I set the initial value of a @FocusState var to a field or a boolean that then selects the text field when the view appears?
var body: some View {
NavigationView {
VStack {
Form {
TextField("Title", text: $viewModel.title)
.focused($titleIsFocused)
}
}
.navigationBarTitle("New List")
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
titleIsFocused = true
}
}
}
}
}