SwiftUI UI Patterns

VerifiedSafe

Best practices and example-driven guidance for building SwiftUI views, including navigation hierarchies, custom modifiers, and responsive layouts.

Sby Skills Guide Bot
DevelopmentIntermediate
7/22/2026
Claude CodeCursor
#swiftui#ios#ui-components#state-management#navigation

Recommended for

Automated summary

Example-driven best practices for building SwiftUI views and components, covering navigation hierarchies, custom view modifiers, responsive layouts, and state ownership.

Strengths

  • Gives a clear decision table for state ownership (@State, @Binding, @Observable, @Environment) with a fallback for iOS 16 and earlier
  • States concrete, opinionated rules for sheets (.sheet(item:), self-dismissing) and scroll-driven reveals
  • Separates guidance for existing projects (match local conventions, find nearby examples) from new-project scaffolding (TabView + NavigationStack + sheets)
  • Points to component-specific reference files instead of forcing one generic pattern

Limitations

  • The provided content cuts off at the start of the cross-cutting references section, so several referenced files are not included
  • Heavily iOS/SwiftUI-specific and not applicable to UIKit, macOS AppKit, or other UI frameworks
  • Assumes working knowledge of Swift and recent SwiftUI APIs such as iOS 17 Observation
When to use it

Reach for it when creating or refactoring SwiftUI screens, designing a tab-based architecture, or deciding which state wrapper fits a given ownership model.

When not to use it

Skip it for UIKit, cross-platform, or web UI work, or when a project mandates a view-model-heavy MVVM structure that this skill explicitly steers away from.

Security analysis

Safe
Quality score88/100

The skill provides declarative guidance for SwiftUI UI development, with no declared tools and no instructions for executing commands, network access, file modification, or data handling. It is purely advisory and poses no meaningful execution risk.

No concerns found

How this analysis is made

Examples

New tabbed app skeleton
Scaffold a new SwiftUI app with a TabView of three tabs (Home, Search, Profile), each wrapping its own NavigationStack, plus a shared router for pushing detail screens. Use the patterns from this skill.
Refactor state ownership
This SwiftUI settings screen uses @ObservedObject and a big view model for everything. Refactor it to use @State, @Binding, and @Observable where appropriate, keeping the same behavior on iOS 17+.
Sheet with selected item
Convert my .sheet(isPresented:) editor into a .sheet(item:) driven by the selected model, and make the sheet own its Save/Cancel actions with dismiss().

name: swiftui-ui-patterns description: Best practices and example-driven guidance for building SwiftUI views and components, including navigation hierarchies, custom view modifiers, and responsive layouts with stacks and grids. Use when creating or refactoring SwiftUI UI, designing tab architecture with TabView, composing screens with VStack/HStack, managing @State or @Binding, building declarative iOS interfaces, or needing component-specific patterns and examples.

SwiftUI UI Patterns

Quick start

Choose a track based on your goal:

Existing project

  • Identify the feature or screen and the primary interaction model (list, detail, editor, settings, tabbed).
  • Find a nearby example in the repo with rg "TabView\(" or similar, then read the closest SwiftUI view.
  • Apply local conventions: prefer SwiftUI-native state, keep state local when possible, and use environment injection for shared dependencies.
  • Choose the relevant component reference from references/components-index.md and follow its guidance.
  • If the interaction reveals secondary content by dragging or scrolling the primary content away, read references/scroll-reveal.md before implementing gestures manually.
  • Build the view with small, focused subviews and SwiftUI-native data flow.

New project scaffolding

  • Start with references/app-wiring.md to wire TabView + NavigationStack + sheets.
  • Add a minimal AppTab and RouterPath based on the provided skeletons.
  • Choose the next component reference based on the UI you need first (TabView, NavigationStack, Sheets).
  • Expand the route and sheet enums as new screens are added.

General rules to follow

  • Use modern SwiftUI state (@State, @Binding, @Observable, @Environment) and avoid unnecessary view models.
  • If the deployment target includes iOS 16 or earlier and cannot use the Observation API introduced in iOS 17, fall back to ObservableObject with @StateObject for root ownership, @ObservedObject for injected observation, and @EnvironmentObject only for truly shared app-level state.
  • Prefer composition; keep views small and focused.
  • Use async/await with .task and explicit loading/error states. For restart, cancellation, and debouncing guidance, read references/async-state.md.
  • Keep shared app services in @Environment, but prefer explicit initializer injection for feature-local dependencies and models. For root wiring patterns, read references/app-wiring.md.
  • Prefer the newest SwiftUI API that fits the deployment target and call out the minimum OS whenever a pattern depends on it.
  • Maintain existing legacy patterns only when editing legacy files.
  • Follow the project's formatter and style guide.
  • Sheets: Prefer .sheet(item:) over .sheet(isPresented:) when state represents a selected model. Avoid if let inside a sheet body. Sheets should own their actions and call dismiss() internally instead of forwarding onCancel/onConfirm closures.
  • Scroll-driven reveals: Prefer deriving a normalized progress value from scroll offset and driving the visual state from that single source of truth. Avoid parallel gesture state machines unless scroll alone cannot express the interaction.

State ownership summary

Use the narrowest state tool that matches the ownership model:

| Scenario | Preferred pattern | | --- | --- | | Local UI state owned by one view | @State | | Child mutates parent-owned value state | @Binding | | Root-owned reference model on iOS 17+ | @State with an @Observable type | | Child reads or mutates an injected @Observable model on iOS 17+ | Pass it explicitly as a stored property | | Shared app service or configuration | @Environment(Type.self) | | Legacy reference model on iOS 16 and earlier | @StateObject at the root, @ObservedObject when injected |

Choose the ownership location first, then pick the wrapper. Do not introduce a reference model when plain value state is enough.

Cross-cutting references

  • references/navigationstack.md: navigation ownership, per-tab history, and enum routing.
  • references/sheets.md: centralized modal presentation and enum-driven sheets.
  • references/deeplinks.md: URL handling and routing external links into app destinations.
  • references/app-wiring.md: root dependency graph, environment usage, and app shell wiring.
  • references/async-state.md: .task, .task(id:), cancellation, debouncing, and async UI state.
  • references/previews.md: #Preview, fixtures, mock environments, and isolated preview setup.
  • references/performance.md: stable identity, observation scope, lazy containers, and render-cost guardrails.

Anti-patterns

  • Giant views that mix layout, business logic, networking, routing, and formatting in one file.
  • Multiple boolean flags for mutually exclusive sheets, alerts, or navigation destinations.
  • Live service calls directly inside body-driven code paths instead of view lifecycle hooks or injected models/services.
  • Reaching for AnyView to work around type mismatches that should be solved with better composition.
  • Defaulting every shared dependency to @EnvironmentObject or a global router without a clear ownership reason.

Workflow for a new SwiftUI view

  1. Define the view's state, ownership location, and minimum OS assumptions before writing UI code.
  2. Identify which dependencies belong in @Environment and which should stay as explicit initializer inputs.
  3. Sketch the view hierarchy, routing model, and presentation points; extract repeated parts into subviews. For complex navigation, read references/navigationstack.md, references/sheets.md, or references/deeplinks.md. Build and verify no compiler errors before proceeding.
  4. Implement async loading with .task or .task(id:), plus explicit loading and error states when needed. Read references/async-state.md when the work depends on changing inputs or cancellation.
  5. Add previews for the primary and secondary states, then add accessibility labels or identifiers when the UI is interactive. Read references/previews.md when the view needs fixtures or injected mock dependencies.
  6. Validate with a build: confirm no compiler errors, check that previews render without crashing, ensure state changes propagate correctly, and sanity-check that list identity and observation scope will not cause avoidable re-renders. Read references/performance.md if the screen is large, scroll-heavy, or frequently updated. For common SwiftUI compilation errors — missing @State annotations, ambiguous ViewBuilder closures, or mismatched generic types — resolve them before updating callsites. If the build fails: read the error message carefully, fix the identified issue, then rebuild before proceeding to the next step. If a preview crashes, isolate the offending subview, confirm its state initialisation is valid, and re-run the preview before continuing.

Component references

Use references/components-index.md as the entry point. Each component reference should include:

  • Intent and best-fit scenarios.
  • Minimal usage pattern with local conventions.
  • Pitfalls and performance notes.
  • Paths to existing examples in the current repo.

Adding a new component reference

  • Create references/<component>.md.
  • Keep it short and actionable; link to concrete files in the current repo.
  • Update references/components-index.md with the new entry.

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/swiftui-ui-patterns-mrwakg7e' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/swiftui-ui-patterns-mrwakg7e/download' --output '.skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md.part' && mv '.skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md.part' '.skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md' && test -s '.skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/swiftui-ui-patterns' && cp -R '.skill-downloads/swiftui-ui-patterns-mrwakg7e' '.claude/skills/swiftui-ui-patterns'

Cursor
test -f '.skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md' && test -s '.skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/swiftui-ui-patterns' && cp -R '.skill-downloads/swiftui-ui-patterns-mrwakg7e' '.cursor/skills/swiftui-ui-patterns'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/swiftui-ui-patterns-mrwakg7e'

openskills read 'swiftui-ui-patterns-mrwakg7e'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/swiftui-ui-patterns-mrwakg7e/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills