Logo for tanaschita.com

Managing focus in SwiftUI with FocusState

Learn how to observe, move, and remove focus in SwiftUI views.

10 Aug 2026 · 4 min read

SwiftUI provides the @FocusState property wrapper to observe and control focus in a view hierarchy. We can use it to focus a text field programmatically, move focus between fields, or remove focus to dismiss the keyboard.

In this article, we'll look at the different ways to represent focus and how to pass focus state between views.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

Managing focus for a single view

When we only need to manage one focusable view, we can represent its focus state with a Bool:

struct NameView: View {
@State private var name = ""
@FocusState private var isNameFocused: Bool
var body: some View {
VStack {
TextField("Name", text: $name)
.focused($isNameFocused)
Button("Edit name") {
isNameFocused = true
}
}
}
}

The focused(_:) modifier connects the text field to isNameFocused. The value changes to true when the text field receives focus and back to false when focus leaves it.

This connection also works in the other direction. Setting the value to true moves focus to the text field. Setting it to false removes focus.

Managing focus for multiple views

For multiple focusable views, a single Boolean is no longer enough because it cannot describe which view is focused. We can use an optional enum instead:

struct LoginView: View {
private enum Field: Hashable {
case email
case password
}
@State private var email = ""
@State private var password = ""
@FocusState private var focusedField: Field?
var body: some View {
Form {
TextField("Email", text: $email)
.textContentType(.emailAddress)
.submitLabel(.next)
.focused($focusedField, equals: .email)
.onSubmit {
focusedField = .password
}
SecureField("Password", text: $password)
.textContentType(.password)
.submitLabel(.done)
.focused($focusedField, equals: .password)
.onSubmit {
focusedField = nil
}
Button("Sign in") {
validate()
}
}
}
private func validate() {
if email.isEmpty {
focusedField = .email
} else if password.isEmpty {
focusedField = .password
} else {
focusedField = nil
signIn()
}
}
}

Each field is connected to its corresponding enum case using focused(_:equals:). When a field receives focus, focusedField is updated with that case. Assigning a case programmatically moves focus to the associated field.

The value is optional because focus can also be outside both fields. Setting it to nil removes focus from all views connected to this property and dismisses the keyboard.

Avoiding ambiguous focus bindings

Each focus value should identify one focus destination. Connecting the same enum case to multiple views makes programmatic focus ambiguous:

TextField("First name", text: $firstName)
.focused($focusedField, equals: .name)
TextField("Last name", text: $lastName)
.focused($focusedField, equals: .name)

When either field receives focus, focusedField becomes .name. But when we assign .name ourselves, SwiftUI cannot determine which field should receive focus. Using a separate enum case for each destination avoids this ambiguity.

Passing focus state to a child view

Sometimes the focusable control is extracted into a child view. In this case, we can pass the projected focus state as a FocusState.Binding:

struct ParentView: View {
@FocusState private var isNameFocused: Bool
var body: some View {
NameField(isFocused: $isNameFocused)
}
}
struct NameField: View {
@State private var name = ""
@FocusState.Binding var isFocused: Bool
var body: some View {
TextField("Name", text: $name)
.focused($isFocused)
}
}

This is useful when the child contains the view whose focus is represented by the property. The child attaches the binding to that view with the focused(_:) modifier, while the parent can still observe and change the focus.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

swiftui

property wrappers

swift

ios

Presenting alerts and confirmation dialogs from identifiable data in SwiftUI

Learn how to present alerts and confirmation dialogs using optional identifiable items instead of Boolean state.

24 Aug 2026 · 4 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum