While using FocusState in SwiftUI, once the return key is pressed keyboard closes and then opens again, how to restrict this behaviour? Once the return/submit key is pressed it should directly focus the next field without disappearing/reappearing keyboard.
→ See Quick Video
Here is the code below –
import SwiftUI
struct ContentView: View {
@State var username: String = ""
@State var email: String = ""
@State var password: String = ""
@FocusState var focus : FocusField?
enum FocusField: Hashable{
case username, email, password
}
var body: some View {
Form{
TextField("Username", text: $username)
.focused($focus, equals: .username)
.onSubmit {
focus = .email
}
TextField("Email", text: $email)
.focused($focus, equals: .email)
.keyboardType(.emailAddress)
.onSubmit {
focus = .password
}
SecureField("Password", text: $password)
.focused($focus, equals: .password)
Button(action: {}, label: {
Text("Submit").padding(4)
}).buttonStyle(.borderedProminent).tint(.black).controlSize(.small).padding(.vertical, 8)
}.onAppear(perform: {
focus = .username
})
}
}
#Preview {
ContentView()
}