Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to ad a onEditingChanged in a secure text field In swiftUi?

I was making a login page for an app I am working on and was adding offset to a textfield to it would move up when they keyboard popped up but when I tried it on the secure field I was not able to select onEditingChanged like I did on the regular textfield. I used this code for the Regular Textfeild:

TextField("Username", text: $username, onEditingChanged: { edit in  if edit == true {self.EditingMode = true} else {self.EditingMode = false }})
        .padding()
        .background(lightGreyColor)
        .cornerRadius(5.0)
        .padding(.bottom, 20)

And for the Secure Field I tried to use the same thing but it wouldn't work

SecureField("Password", text: $password)
  .padding()
  .background(lightGreyColor)
  .cornerRadius(5.0)
  .padding(.bottom, 20)
like image 747
Zoren Singh Avatar asked Jul 03 '20 01:07

Zoren Singh


Video Answer


2 Answers

You could use the onTapGesture modifier

.onTapGesture {
    print("Password Field clicked")
}
like image 84
Ludyem Avatar answered Oct 10 '22 13:10

Ludyem


@State private var isEditing: Bool = false

var body: some View {

   SecureField("Password", text: $password, onCommit: { isEditing = false })
        .onTapGesture {
            isEditing = true
        }
   }
}
like image 1
shawnynicole Avatar answered Oct 10 '22 12:10

shawnynicole