3

I have a Group of a SecureField and a TextField in order to simulate the ability to toggle the password from hidden to showing. The TextField works as intended even if I go to another field or anywhere on the view so it makes me think that my problem isn't with my states (I'm using a password Published var in a viewModel I take from the Environment).

With the Secure field though (so when the password is not visible) if I have already typed and then lose focus and come back to it and type again it resets. It's also the same if I remove the TextField and keep only the SecureField in my Group. I wonder if it's the default functionality of the SecureField or I have to add a modifier I can't find.

To give an even better picture, I have a didSet on my variable and it shows the following see console log(printing twice on each activity I think because of the TextField). The 4*aaaa is 2 for the first time changed to aaaa and the next 2 for when I click in the above TextField. Then when I reclick to the SecureField nothing happens and when I type a again the last four lines are printed.

Thanks in advance for any help you might provide. :)

Group{
    if (!loginViewModel.showPassword) {
        SecureField("password".localizableString(appViewModel.language),
                     text: $loginViewModel.password)
    }else {
        TextField("password".localizableString(appViewModel.language), 
                   text: $loginViewModel.password)
          .autocapitalization(.none)
          .padding(.vertical, -0.75)
    }
}
.disableAutocorrection(true)
.padding(.vertical, -2)
.modifier(PlaceholderStyle(showPlaceHolder: loginViewModel.password.isEmpty, placeholder: "password".localizableString(appViewModel.language)))
.foregroundColor(Color("TextDefault"))
.keyboardType(.emailAddress)
kleo
  • 31
  • 2
  • 1
    Please include code as text, not images, which can't be copied/pasted, searched, or read by screen readers. – jnpdx Dec 02 '22 at 14:39
  • just did now :) – kleo Dec 02 '22 at 15:01
  • Well, I avoided the main problem by changing the Group to a ZStack and conditionally changing the opacity between the Secure and TextField. Still, though when the SecureField regains focus (even by reclicking to it) the value resets. – kleo Dec 02 '22 at 15:13
  • 1
    `SecureField` works like that also on your iPhone: when you leave the password field and then comes back, you have to re-type the password. – HunterLion Dec 02 '22 at 15:52
  • My guess is that you are misusing `StateObject` and `ObservedObject`. `StateObject` is for initializing and `ObservedObject` is for passing around. More often than not people with this issue are initializing with `ObservedObject` – lorem ipsum Dec 02 '22 at 18:03
  • @HunterLion Well if that's the case there are no bugs. How could I avoid the reset thought? Maybe with a UIKit import instead? I want it to be pure SwiftUI if possible. – kleo Dec 03 '22 at 18:49

0 Answers0