You have to add a large delay for @FocusState to work inside of a sheet. Here is a basic example.
enum FocusableField: Hashable {
case email
case password
}
struct ContentView: View {
@State private var show = false
var body: some View {
Button("Show"){
show.toggle()
}
.sheet(isPresented: $show){
SheetView()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewLayout(.sizeThatFits)
}
}
struct SheetView: View{
@State private var email = ""
@State private var password = ""
@FocusState private var focus: FocusableField?
var body: some View{
NavigationView {
Form {
TextField("email", text: $email, prompt: Text("email"))
.focused($focus, equals: .email)
SecureField("password", text: $password, prompt: Text("password"))
.focused($focus, equals: .password)
}
.navigationTitle("Sign in")
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
focus = .email
}
}
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: