Home > Software engineering >  How to detect when a TextField becomes active in SwiftUI?
How to detect when a TextField becomes active in SwiftUI?

Time:01-24

.onTapGesture {
   // do something
}

Doesn't work because TextFields can be tapped without beginning editing mode.

CodePudding user response:

The answer is to initialize TextField with the onEditingChanged parameter.

We can then execute a closure conditionally depending upon whether the text field was edited or changes were committed:

TextField("", text: $email, onEditingChanged: { changed in
  if changed {
    // User began editing the text field
  }
  else {
    // User tapped the return key
  }
})

CodePudding user response:

Have you tried with onEditingChanged

    .onEditingChanged {
         //do something cool
    }
  •  Tags:  
  • Related