Hello,
I have a view has two textfields(a and b) and a button.
I want to move focus between textfields using enter key. that is;
enter value on a -> presss enter key -> enter value on b
How can I do it in swiftUI?
if someone give me an advice about it, I'd de very appreciated.
thanks,
c00012
Use FocusState.
Here is a simple code example:
struct ContentView: View {
let textCase1 = 1
let textCase2 = 2
@State var text1: String = ""
@State var text2: String = ""
@FocusState private var focusedField: Int?
var body: some View {
VStack {
HStack {
Text(" TextField 1")
TextField("", text: $text1)
.border(Color.blue, width: 1)
.focused($focusedField, equals: textCase1)
.onSubmit {
focusedField = 2
}
}
HStack {
Text(" TextField 2")
TextField("", text: $text2)
.border(Color.red, width: 1)
.focused($focusedField, equals: textCase2)
.onSubmit {
focusedField = 1
}
}
Button {
focusedField = nil
print("No more focus")
} label: {
Text("Do something else")
}
}
}
}