
SwiftUI UI Patterns
SkillSwiftUI 界面构建的实践指南:按状态归属选择 @State、@Binding、@Observable 或 @Environment,用 TabView、NavigationStack 与 sheet 搭建应用骨架,用 .task 处理异步加载,拆小视图、写 Preview,并列出常见反模式;可按组件索引查阅具体组件的用法与示例。
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.mdand follow its guidance. - If the interaction reveals secondary content by dragging or scrolling the primary content away, read
references/scroll-reveal.mdbefore implementing gestures manually. - Build the view with small, focused subviews and SwiftUI-native data flow.
New project scaffolding
- Start with
references/app-wiring.mdto wire TabView + NavigationStack + sheets. - Add a minimal
AppTabandRouterPathbased 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
ObservableObjectwith@StateObjectfor root ownership,@ObservedObjectfor injected observation, and@EnvironmentObjectonly for truly shared app-level state. - Prefer composition; keep views small and focused.
- Use async/await with
.taskand explicit loading/error states. For restart, cancellation, and debouncing guidance, readreferences/async-state.md. - Keep shared app services in
@Environment, but prefer explicit initializer injection for feature-local dependencies and models. For root wiring patterns, readreferences/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. Avoidif letinside a sheet body. Sheets should own their actions and calldismiss()internally instead of forwardingonCancel/onConfirmclosures. - 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
AnyViewto work around type mismatches that should be solved with better composition. - Defaulting every shared dependency to
@EnvironmentObjector a global router without a clear ownership reason.
Workflow for a new SwiftUI view
- Define the view's state, ownership location, and minimum OS assumptions before writing UI code.
- Identify which dependencies belong in
@Environmentand which should stay as explicit initializer inputs. - Sketch the view hierarchy, routing model, and presentation points; extract repeated parts into subviews. For complex navigation, read
references/navigationstack.md,references/sheets.md, orreferences/deeplinks.md. Build and verify no compiler errors before proceeding. - Implement async loading with
.taskor.task(id:), plus explicit loading and error states when needed. Readreferences/async-state.mdwhen the work depends on changing inputs or cancellation. - Add previews for the primary and secondary states, then add accessibility labels or identifiers when the UI is interactive. Read
references/previews.mdwhen the view needs fixtures or injected mock dependencies. - 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.mdif the screen is large, scroll-heavy, or frequently updated. For common SwiftUI compilation errors — missing@Stateannotations, ambiguousViewBuilderclosures, 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.mdwith the new entry.
相关资源

Build iOS Apps
SkillOpenAI 官方 iOS 技能组:SwiftUI 界面模式与视图重构、iOS 26 Liquid Glass、App Intents、性能审计与 ETTrace 采样、用 memgraph 排查内存泄漏、模拟器调试。需要 macOS 与 Xcode,调试类技能依赖 XcodeBuildMCP。
openai/plugins7.5k
SwiftUI Pro
SkillHacking with Swift 作者 Paul Hudson 编写的 SwiftUI 技能:审查并改进 SwiftUI 代码,覆盖现代与已弃用 API、性能、导航与布局、状态管理和 VoiceOver 无障碍,面向 iOS 26 与 Swift 6.2。
twostraws/SwiftUI-Agent-Skill6.2k
Apple Design
Skill以 Apple 人机界面指南为依据(内置从 developer.apple.com 抓取的 123 页原文)审查移动端与桌面应用的界面设计,从无障碍、平台惯例等五个角度逐项引用条款给出意见,同时指出模板化、缺少个性的地方。可审截图、设计稿、Figma 导出或界面代码,适用于 SwiftUI、UIKit、Flutter、React Native、Tauri、Electron。
dickwu/apple-design-skill1.5k
Build iOS Apps
PluginOpenAI 官方 Codex 插件,覆盖 SwiftUI 界面与重构、App Intents、模拟器调试、性能剖析与内存泄漏排查,内置 XcodeBuildMCP。需要 macOS 与 Xcode,仅支持 Codex。
openai/plugins7.5k
UI UX Pro Max
Skill界面设计知识库技能:按产品类型检索界面风格、配色、字体搭配、图表与 UX 规范,并生成完整设计系统,覆盖 React、SwiftUI、Flutter 等 22 种技术栈。需本机安装 Python 3。
nextlevelbuilder/ui-ux-pro-max-skill136k
App Store & Google Play Screenshots Generator
Skill让 agent 在本机搭一个 Next.js 截图编辑器,把应用截图做成 App Store 和 Google Play 宣传图。支持真实设备外框、大字卖点文案、18 种视觉风格、多语言和从右到左排版,一键导出各商店要求的全部尺寸。需要本机装好 Node.js。
ParthJadhav/app-store-screenshots7.3k