My requirement is that, I have one textField, onpressing it one keyboard will appear. In keyboard, the nextButton is pressed it should go to next textfield without dismissing the textField.I have tried with FocusState, but it is not working
import SwiftUI
enum placeholder {
case email
case password
var description: String {
return switch self {
case .email: "email"
case .password: "password"
}
}
var nextControll: String {
return switch self {
case .email: "password"
case .password: "email"
}
}
}
struct customTextField: View {
var placeHolder: placeholder = .email
@State var description: String = ""
@FocusState var focusstate: String?
var body: some View {
TextField(placeHolder.description, text: $description)
.padding(.horizontal, 20)
.frame(width: 300, height: 50)
.background {
RoundedRectangle(cornerRadius: 12)
.stroke(Color.gray, lineWidth: 2)
}
.focused($focusstate, equals: placeHolder.description)
.onSubmit {
focusstate = placeHolder.nextControll
}
.submitLabel(.next)
}
}
struct TextFieldObservers: View {
var placeholderView: placeholder?
var body: some View {
Group {
customTextField(placeHolder: .email, description: "")
customTextField(placeHolder: .password, description: "")
}
.padding(.horizontal, 30)
}
}
#Preview {
TextFieldObservers()
}
See reference link: https://www.reddit.com/r/SwiftUI/comments/xxvvo3/how_to_prevent_the_keyboard_hiding_after_i_click/?rdt=61416
Each of your customTextField
's has own @FocusState
, they don't share it with each other, your TextFieldObservers
should have that focus state and text fields - receive it as a parameter. There are a lot of other options tow to organize it as well