I trying to make my textField to secureField but inProcess When I tapped on eye button. keyBoard is getting dismissed, which I don’t want. Here is the code I wrote
import SwiftUI
enum TextFieldTypes {
case name
case password
}
struct SecureFieldTest: View {
@State var name: String = ""
@State var password: String = ""
@State var isEyeSlashed: Bool = false
@FocusState var focusState: TextFieldTypes?
var body: some View {
VStack {
MyTextField(placeHolder: "name",
text: $name,
focusState: $focusState,
currentFocuState: .name,
nextFocusState: .password)
MyTextField(placeHolder: "password",
text: $password,
focusState: $focusState,
currentFocuState: .password,
nextFocusState: .name)
}
.textFieldStyle(.roundedBorder)
}
}
struct MyTextField: View {
var placeHolder: String
@Binding var text: String
@FocusState.Binding var focusState: TextFieldTypes?
@State var currentFocuState: TextFieldTypes
@State var nextFocusState: TextFieldTypes
@State var isEyeSlashed: Bool = false
var body: some View {
HStack {
if isEyeSlashed {
SecureField(placeHolder, text: $text)
} else {
TextField(placeHolder, text: $text)
}
Image(systemName: isEyeSlashed ? "eye.slash": "eye")
.onTapGesture {
isEyeSlashed.toggle()
}
}
.focused($focusState, equals: currentFocuState)
.onSubmit {
focusState = nextFocusState
}
}
}
#Preview {
SecureFieldTest()
}
I am thinking that i have to implement my own secureField instead of swiftUi’s one. Any other solutions?