ios-navigation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseApple iOS Navigation Best Practices
Apple iOS 导航最佳实践
Comprehensive guide for building fluid, golden-standard navigation in iOS apps with SwiftUI. Contains 51 rules across 8 categories covering architecture, anti-patterns, transitions, modals, flow orchestration, performance, accessibility, and state restoration.
这是一份使用SwiftUI在iOS应用中构建流畅、黄金标准导航的综合指南。包含8个类别下的51条规则,覆盖架构、反模式、过渡、模态视图、流程编排、性能、无障碍访问和状态恢复。
When to Apply
适用场景
Reference these guidelines when:
- Designing navigation hierarchies with NavigationStack or NavigationSplitView
- Choosing between push, sheet, and fullScreenCover
- Implementing hero animations, zoom transitions, or gesture-driven dismissals
- Building multi-step flows (onboarding, checkout, registration)
- Using @Observable with @Environment and @Bindable for shared navigation state
- Reviewing code for navigation anti-patterns
- Adding deep linking, state restoration, or tab persistence
- Ensuring VoiceOver and reduce motion support for navigation
在以下场景中参考这些指南:
- 使用NavigationStack或NavigationSplitView设计导航层级
- 在push、sheet和fullScreenCover之间做选择
- 实现英雄动画、缩放过渡或手势驱动的关闭操作
- 构建多步骤流程(引导页、结账、注册)
- 结合@Environment和@Bindable使用@Observable来管理共享导航状态
- 检查导航代码中的反模式
- 添加深度链接、状态恢复或标签页持久化
- 确保导航支持VoiceOver和减少动态效果
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Navigation Architecture | CRITICAL | |
| 2 | Navigation Anti-Patterns | CRITICAL | |
| 3 | Transition & Animation | HIGH | |
| 4 | Modal Presentation | HIGH | |
| 5 | Flow Orchestration | HIGH | |
| 6 | Navigation Performance | MEDIUM-HIGH | |
| 7 | Navigation Accessibility | MEDIUM | |
| 8 | State & Restoration | MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 导航架构 | 关键 | |
| 2 | 导航反模式 | 关键 | |
| 3 | 过渡与动画 | 高 | |
| 4 | 模态视图呈现 | 高 | |
| 5 | 流程编排 | 高 | |
| 6 | 导航性能 | 中高 | |
| 7 | 导航无障碍访问 | 中 | |
| 8 | 状态与恢复 | 中 | |
Quick Reference
快速参考
1. Navigation Architecture (CRITICAL)
1. 导航架构(关键)
- - Use NavigationStack over deprecated NavigationView
arch-navigation-stack - - Use value-based NavigationLink over destination closures
arch-value-based-links - - Register navigationDestination at stack root
arch-destination-registration - - Use navigationDestination(item:) for optional-based navigation (iOS 17+)
arch-destination-item - - Define routes as Hashable enums
arch-route-enum - - Use NavigationSplitView for multi-column layouts
arch-split-view - - Extract navigation logic into Observable coordinator
arch-coordinator - - Use @Environment with @Observable and @Bindable for shared state
arch-observable-environment - - Handle deep links by appending to NavigationPath
arch-deep-linking - - Use NavigationPath for heterogeneous type-erased navigation
arch-navigation-path
- - 使用NavigationStack替代已弃用的NavigationView
arch-navigation-stack - - 使用基于值的NavigationLink而非目标闭包
arch-value-based-links - - 在栈根注册navigationDestination
arch-destination-registration - - 使用navigationDestination(item:)实现基于可选值的导航(iOS 17+)
arch-destination-item - - 将路由定义为Hashable枚举
arch-route-enum - - 使用NavigationSplitView实现多列布局
arch-split-view - - 将导航逻辑提取到Observable协调器中
arch-coordinator - - 结合@Environment与@Observable和@Bindable使用共享状态
arch-observable-environment - - 通过向NavigationPath追加内容处理深度链接
arch-deep-linking - - 使用NavigationPath实现异构类型擦除的导航
arch-navigation-path
2. Navigation Anti-Patterns (CRITICAL)
2. 导航反模式(关键)
- - Avoid mixing NavigationLink(destination:) with NavigationLink(value:)
anti-mixed-link-styles - - Avoid scattering navigationDestination across views
anti-scattered-destinations - - Avoid sharing NavigationStack across tabs
anti-shared-stack - - Avoid hiding back button without preserving swipe gesture
anti-hidden-back-button - - Avoid heavy work in view initializers
anti-navigation-in-init - - Avoid hamburger menu navigation
anti-hamburger-menu - - Avoid programmatic tab selection changes
anti-programmatic-tab-switch
- - 避免混合使用NavigationLink(destination:)与NavigationLink(value:)
anti-mixed-link-styles - - 避免在多个视图中分散定义navigationDestination
anti-scattered-destinations - - 避免在多个标签页之间共享NavigationStack
anti-shared-stack - - 避免隐藏返回按钮而不保留滑动返回手势
anti-hidden-back-button - - 避免在视图初始化器中执行繁重操作
anti-navigation-in-init - - 避免使用汉堡菜单导航
anti-hamburger-menu - - 避免以编程方式修改标签页选择
anti-programmatic-tab-switch
3. Transition & Animation (HIGH)
3. 过渡与动画(高)
- - Use zoom navigation transition for hero animations (iOS 18+)
anim-zoom-transition - - Use matchedGeometryEffect only within same view hierarchy
anim-matched-geometry-same-view - - Use modern spring animation syntax (iOS 17+)
anim-spring-config - - Use interactive spring animations for gesture-driven transitions
anim-gesture-driven - - Style transition sources with shape and background
anim-transition-source-styling - - Respect reduce motion for all navigation animations
anim-reduce-motion-transitions - - Use onScrollGeometryChange for scroll-driven transitions (iOS 18+)
anim-scroll-driven
- - 使用缩放导航过渡实现英雄动画(iOS 18+)
anim-zoom-transition - - 仅在同一视图层级内使用matchedGeometryEffect
anim-matched-geometry-same-view - - 使用现代弹簧动画语法(iOS 17+)
anim-spring-config - - 为手势驱动的过渡使用交互式弹簧动画
anim-gesture-driven - - 为过渡源添加形状和背景样式
anim-transition-source-styling - - 所有导航动画均需遵循减少动态效果的设置
anim-reduce-motion-transitions - - 使用onScrollGeometryChange实现滚动驱动的过渡(iOS 18+)
anim-scroll-driven
4. Modal Presentation (HIGH)
4. 模态视图呈现(高)
- - Use push for drill-down, sheet for supplementary content
modal-sheet-vs-push - - Use presentation detents for contextual sheet sizing
modal-detents - - Use fullScreenCover only for immersive standalone experiences
modal-fullscreen-cover - - Place .sheet on container view, not on NavigationLink
modal-sheet-placement - - Guard unsaved changes with interactiveDismissDisabled
modal-interactive-dismiss - - Use separate NavigationStack inside modals
modal-nested-navigation
- - 向下钻取使用push,补充内容使用sheet
modal-sheet-vs-push - - 使用presentation detents实现上下文相关的sheet尺寸
modal-detents - - 仅在沉浸式独立体验中使用fullScreenCover
modal-fullscreen-cover - - 将.sheet添加到容器视图,而非NavigationLink
modal-sheet-placement - - 使用interactiveDismissDisabled保护未保存的更改
modal-interactive-dismiss - - 在模态视图内部使用独立的NavigationStack
modal-nested-navigation
5. Flow Orchestration (HIGH)
5. 流程编排(高)
- - Give each tab its own NavigationStack
flow-tab-independence - - Use NavigationStack with route array for multi-step flows
flow-multi-step - - Use NavigationSplitView with selection binding for sidebar
flow-sidebar-navigation - - Use sidebarAdaptable TabView for iPad tab-to-sidebar (iOS 18+)
flow-tab-sidebar-adaptive - - Implement pop-to-root by clearing NavigationPath
flow-pop-to-root - - Keep screens independent of parent navigation context
flow-screen-independence
- - 为每个标签页提供独立的NavigationStack
flow-tab-independence - - 使用带路由数组的NavigationStack实现多步骤流程
flow-multi-step - - 使用带选择绑定的NavigationSplitView实现侧边栏
flow-sidebar-navigation - - 使用sidebarAdaptable TabView实现iPad的标签页转侧边栏(iOS 18+)
flow-tab-sidebar-adaptive - - 通过清空NavigationPath实现返回根视图
flow-pop-to-root - - 保持屏幕独立于父导航上下文
flow-screen-independence
6. Navigation Performance (MEDIUM-HIGH)
6. 导航性能(中高)
- - Use value-based NavigationLink for lazy destination construction
perf-lazy-destinations - - Use .task for async data loading on navigation
perf-task-modifier - - Own @Observable state with @State, pass as plain property
perf-state-object-ownership - - Avoid side effects in view body
perf-avoid-body-side-effects - - Use LazyView wrapper for pre-iOS 16 destination loading
perf-lazy-view-wrapper
- - 使用基于值的NavigationLink实现目标视图的延迟构建
perf-lazy-destinations - - 使用.task在导航时加载异步数据
perf-task-modifier - - 使用@State持有@Observable状态,以普通属性传递
perf-state-object-ownership - - 避免在视图body中产生副作用
perf-avoid-body-side-effects - - 使用LazyView包装器实现iOS 16之前的目标视图延迟加载
perf-lazy-view-wrapper
7. Navigation Accessibility (MEDIUM)
7. 导航无障碍访问(中)
- - Mark navigation section headers for VoiceOver rotor
ally-rotor-headers - - Manage focus after programmatic navigation events
ally-focus-after-navigation - - Group related navigation elements to reduce swipe count
ally-group-navigation-elements - - Hide decorative navigation elements from VoiceOver
ally-hide-decorative-navigation - - Use @FocusState for keyboard navigation in forms
ally-keyboard-focus
- - 为VoiceOver转子标记导航章节标题
ally-rotor-headers - - 在编程式导航事件后管理焦点
ally-focus-after-navigation - - 对相关导航元素进行分组以减少滑动次数
ally-group-navigation-elements - - 向VoiceOver隐藏装饰性导航元素
ally-hide-decorative-navigation - - 在表单中使用@FocusState实现键盘导航
ally-keyboard-focus
8. State & Restoration (MEDIUM)
8. 状态与恢复(中)
- - Make route enums Codable for navigation persistence
state-codable-routes - - Use SceneStorage for per-scene navigation persistence
state-scene-storage - - Persist selected tab with SceneStorage
state-tab-persistence - - Parse deep link URLs into route enums
state-deep-link-urls - - Avoid defining NavigationPath at App level
state-avoid-app-level-path
- - 将路由枚举设为Codable以实现导航持久化
state-codable-routes - - 使用SceneStorage实现每个场景的导航持久化
state-scene-storage - - 使用SceneStorage持久化选中的标签页
state-tab-persistence - - 将深度链接URL解析为路由枚举
state-deep-link-urls - - 避免在App级别定义NavigationPath
state-avoid-app-level-path
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |