cbusch
cbusch

Reputation: 325

How to detect when a TextField becomes active in SwiftUI?

.onTapGesture {
   // do something
}

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

Upvotes: 2

Views: 4265

Answers (3)

Saurabh Pathak
Saurabh Pathak

Reputation: 1499

You Should try with it ,

onEditingChanged: { (isBegin) in
                if isBegin {
                    print("Begins editing")
                } else {
                    print("Finishes editing")
                }
            },

Upvotes: -1

cbusch
cbusch

Reputation: 325

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
  }
})

Upvotes: 9

Jimmy J
Jimmy J

Reputation: 24

Have you tried with onEditingChanged

    .onEditingChanged {
         //do something cool
    }

Upvotes: -1

Related Questions