How to detect when a TextField loses the focus in SwiftUI for iOS?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Detecting when a SwiftUI TextField loses focus is usually about validation, saving edits, or advancing a form. In current SwiftUI, the cleanest way is to bind focus explicitly with @FocusState and react when that state changes from active to inactive.
Detect blur with @FocusState
On iOS 15 and later, @FocusState is the standard API for focus management. You bind the field to a Boolean or enum-backed focus value, then watch that binding for changes.
When the Boolean changes to false, the field has lost focus. That can happen because the user tapped elsewhere, moved to another field, dismissed the keyboard, or because your code changed focus programmatically.
Manage several fields with one enum
A single Boolean works for one field, but multi-field forms are easier to reason about with an enum.
This pattern scales better because only one field is focused at a time, and the state is represented explicitly.
Validate when focus leaves the field
Blur detection is often used to delay validation until the user finishes editing. That avoids noisy validation on every keystroke.
That keeps the form responsive while still giving feedback at the right moment.
Programmatic focus changes count too
Because focus is a normal state value, you can set it in code and reuse the same blur handling.
That makes keyboard dismissal, wizard-style navigation, and validation flow consistent. The blur logic does not care whether focus changed because of a tap or because your code updated the state.
Older deployment targets
If you support versions before @FocusState, SwiftUI does not offer the same direct focus API. In that case, a UIViewRepresentable wrapper around UITextField can expose UIKit delegate callbacks such as textFieldDidEndEditing.
That fallback is more verbose, so it is best reserved for older targets or hybrid UIKit-SwiftUI screens.
Common Pitfalls
The most common mistake is confusing onSubmit with focus loss. onSubmit fires when the user triggers submit behavior, such as pressing return, but a field can lose focus in several other ways.
Another issue is modeling each field with a separate Boolean when the screen really has one active field at a time. Enum-backed focus state is usually simpler and avoids contradictory states.
Developers also forget that programmatic focus changes trigger the same blur logic. If validation should happen only for user-driven blur, add an explicit guard rather than assuming the callback source.
Finally, keep deployment targets in mind. @FocusState is the right answer for modern SwiftUI, but older iOS versions require UIKit bridging if true blur detection is still needed.
Summary
- Use
@FocusStateto track whether a SwiftUITextFieldcurrently has focus. - Observe the focus binding with
onChangeand react when it becomes inactive. - Prefer an enum-backed focus state for multi-field forms.
- Run validation on blur when you want quieter, more natural feedback.
- Use a UIKit bridge only when older deployment targets make
@FocusStateunavailable.

