
Focus Engine
Implement correct keyboard, remote, and gaze focus across SwiftUI and UIKit when building Apple-platform apps.
Install
npx skills add https://github.com/dpearson2699/swift-ios-skills --skill focus-engineWhat is this skill?
- Covers SwiftUI @FocusState, defaultFocus, focused values, and focusable interactions
- Documents tvOS geometric focus, watchOS Digital Crown, visionOS gaze/hover, and macOS key view loop
- Includes focus restoration after presentations and UIKit UIFocusGuide routing
- Common-mistakes section plus a review checklist before merging focus changes
- Points VoiceOver and Switch Control focus to the separate ios-accessibility skill
Adoption & trust: 676 installs on skills.sh; 713 GitHub stars; 3/3 security scanners passed (skills.sh audits).
Recommended Skills
Vercel React Native Skillsvercel-labs/agent-skills
Firebase Basicsfirebase/agent-skills
Building Native Uiexpo/skills
Firebase Ai Logic Basicsfirebase/agent-skills
Native Data Fetchingexpo/skills
Firebase Firestorefirebase/agent-skills
Journey fit
Common Questions / FAQ
Is Focus Engine safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Focus Engine
# Focus Engine Focus behavior for SwiftUI and UIKit apps targeting iOS 26+, iPadOS, macOS, and tvOS. Covers keyboard focus, directional focus, scene-focused values, focus restoration, and UIKit focus guides. `focusSection()` guidance in this skill applies to macOS and tvOS. Accessibility-specific focus for VoiceOver and Switch Control lives in the `ios-accessibility` skill. ## Contents - [SwiftUI FocusState](#swiftui-focusstate) - [Default Focus](#default-focus) - [Focused Values and Scene Values](#focused-values-and-scene-values) - [Focusable Interactions](#focusable-interactions) - [Focus Sections](#focus-sections) - [Focus Restoration](#focus-restoration) - [UIKit Focus Guides](#uikit-focus-guides) - [Common Mistakes](#common-mistakes) - [Review Checklist](#review-checklist) - [References](#references) ## SwiftUI FocusState Use `@FocusState` to read and write focus placement inside a scene. Use `Bool` for a single target or an optional `Hashable` enum for multiple targets. ```swift struct LoginView: View { enum Field: Hashable { case email, password } @State private var email = "" @State private var password = "" @FocusState private var focusedField: Field? var body: some View { Form { TextField("Email", text: $email) .focused($focusedField, equals: .email) SecureField("Password", text: $password) .focused($focusedField, equals: .password) } .onAppear { focusedField = .email } .onSubmit { switch focusedField { case .email: focusedField = .password case .password, nil: submit() } } } } ``` Keep focus state local to the view that owns the focusable controls. ## Default Focus Use `.defaultFocus` to set the preferred initial focus region or control when a view appears or when focus is reassigned automatically. ```swift struct SidebarView: View { enum Target: Hashable { case library, settings } @FocusState private var focusedTarget: Target? var body: some View { VStack { Button("Library") { } .focused($focusedTarget, equals: .library) Button("Settings") { } .focused($focusedTarget, equals: .settings) } .defaultFocus($focusedTarget, .library) } } ``` Prefer one clear default destination per screen or focus region. ## Focused Values and Scene Values Use focused values to expose state from the currently focused view. Use scene-focused values when commands or scene-wide UI should keep access to the value even after focus moves within that scene. ```swift struct SelectedRecipeKey: FocusedValueKey { typealias Value = Binding<Recipe> } extension FocusedValues { var selectedRecipe: Binding<Recipe>? { get { self[SelectedRecipeKey.self] } set { self[SelectedRecipeKey.self] = newValue } } } struct RecipeDetailView: View { @Binding var recipe: Recipe var body: some View { Text(recipe.title) .focusedSceneValue(\.selectedRecipe, $recipe) } } ``` Use this pattern for menus, commands, and toolbars that need to act on the focused scene's current content. ## Focusable Interactions Use `.focusable(_:interactions:)` on custom SwiftUI views that should participate in keyboard or directional focus. ```swift struct SelectableCard: View { let title: String let ac