Reading time: 2 min
Detecting if a text field gained or lost focus is not entirely straightforward in SwiftUI.
TextField
constructor has a parameter named onEditingChanged
, but it triggers only when the user taps the return key in the virtual keyboard, not when the focus is actually lost.
Even worse, SecureField
doesn't have such a parameter, and there's no way to know if the user is interacting with the view!
Search our recipes
Check out the companion apps!
Our free companion app and XCode Extension brings 100+ SwiftUI recipes right at your fingertips! Get a preview of it in the Online Companion.
Have a recipe for us?
Get in touch and let's grow the cookbook together!