Swiftui Navigation

作者 dpearson26998d90fd121a26无许可证1.1K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库2个月前更新

Implement SwiftUI navigation patterns including NavigationStack, NavigationSplitView, sheet presentation, tab-based navigation, and deep linking. Use when building push navigation, programmatic routing, multi-column layouts, modal sheets, tab bars, universal links, or custom URL scheme handling.

AI 生成的概览

指导 SwiftUI 导航实现:NavigationStack、分栏视图、表单弹窗、标签页与深度链接。

功能
该技能为 iOS 应用中的 SwiftUI 导航实现提供参考指导,涵盖使用 NavigationStack 的压栈导航、多列 NavigationSplitView 布局、表单弹窗呈现、基于标签页的架构以及深度链接。它提供代码模式、常见错误清单和审查检查表,并在导航栈、表单弹窗、标签页视图和深度链接等独立参考文件中有更详细的内容。它产出的是文字指导和示例代码,而不是运行工具。
适用场景
适用于构建或审查 SwiftUI 导航时,例如程序化路由、多列布局、模态表单弹窗、标签栏、通用链接或自定义 URL scheme 处理。它面向使用 Swift 6 并发、针对较新 iOS 版本的应用。
运行要求
不需要脚本或工具,仅为说明文档和参考文件。其指导假定使用 SwiftUI 开发,采用 Swift 6.3 与 iOS 26+ API,并在注明处向后兼容至 iOS 17。

SwiftUI Navigation

Navigation patterns for SwiftUI apps targeting iOS 26+ with Swift 6.3. Covers push navigation, multi-column layouts, sheet presentation, tab architecture, and deep linking. Patterns are backward-compatible to iOS 17 unless noted.

Contents

NavigationStack (Push Navigation)

Use NavigationStack with a typed [Route] binding for programmatic push navigation. Define routes as a Hashable enum and map them with .navigationDestination(for:); this keeps the path compile-time checked. Use NavigationPath only when one stack must hold heterogeneous route value types.

swift
enum Route: Hashable {    case item(id: Item.ID)}
struct ContentView: View {    @State private var path: [Route] = []    let items: [Item]
    var body: some View {        NavigationStack(path: $path) {            List(items) { item in                NavigationLink(value: Route.item(id: item.id)) {                    ItemRow(item: item)                }            }            .navigationDestination(for: Route.self) { route in                switch route {                case .item(let id):                    DetailView(itemID: id)                }            }            .navigationTitle("Items")        }    }}

Programmatic navigation:

swift
path.append(.item(id: item.id))  // Pushpath.removeLast()                // Pop onepath = []                        // Pop to root

Router pattern: For apps with complex navigation, use a router object that owns the path and sheet state. Each tab gets its own router instance injected via .environment(). Centralize destination mapping with a single .navigationDestination(for:) block or a shared withAppRouter() modifier.

See references/navigationstack.md [blocked] for full router examples including per-tab stacks, centralized destination mapping, and generic tab routing.

NavigationSplitView (Multi-Column)

Use NavigationSplitView for sidebar-detail layouts on iPad and Mac. Falls back to stack navigation on iPhone.

swift
struct MasterDetailView: View {    @State private var selectedItem: Item?
    var body: some View {        NavigationSplitView {            List(items, selection: $selectedItem) { item in                NavigationLink(value: item) { ItemRow(item: item) }            }            .navigationTitle("Items")        } detail: {            if let item = selectedItem {                ItemDetailView(item: item)            } else {                ContentUnavailableView("Select an Item", systemImage: "sidebar.leading")            }        }    }}

Custom Split Column (Manual HStack)

For custom multi-column layouts (e.g., a dedicated notification column independent of selection), use a manual HStack split with horizontalSizeClass checks:

swift
@MainActorstruct AppView: View {  @Environment(\.horizontalSizeClass) private var horizontalSizeClass  @AppStorage("showSecondaryColumn") private var showSecondaryColumn = true
  var body: some View {    HStack(spacing: 0) {      primaryColumn      if shouldShowSecondaryColumn {        Divider().edgesIgnoringSafeArea(.all)        secondaryColumn      }    }  }
  private var shouldShowSecondaryColumn: Bool {    horizontalSizeClass == .regular      && showSecondaryColumn  }
  private var primaryColumn: some View {    TabView { /* tabs */ }  }
  private var secondaryColumn: some View {    NotificationsTab()      .environment(\.isSecondaryColumn, true)      .frame(maxWidth: .secondaryColumnWidth)  }}

Use the manual HStack split when you need full control or a non-standard secondary column. Use NavigationSplitView when you want a standard system layout with minimal customization.

Sheet Presentation

Prefer .sheet(item:) over .sheet(isPresented:) when state represents a selected model. Sheets should own their actions and call dismiss() internally.

swift
@State private var selectedItem: Item?
.sheet(item: $selectedItem) { item in    EditItemSheet(item: item)}

Presentation sizing (iOS 18+): Control sheet dimensions with .presentationSizing:

swift
.sheet(item: $selectedItem) { item in    EditItemSheet(item: item)        .presentationSizing(.form)  // .form, .page, .fitted, .automatic}

PresentationSizing values:

  • .automatic -- platform default
  • .page -- roughly paper size, for informational content
  • .form -- slightly narrower than page, for form-style UI
  • .fitted -- sized by the content's ideal size

Fine-tuning: .fitted(horizontal:vertical:) constrains fitting axes; .sticky(horizontal:vertical:) grows but does not shrink in specified dimensions.

Dismissal protection: On iOS/iPadOS, use .interactiveDismissDisabled(hasUnsavedChanges) and provide explicit Save/Discard actions inside the sheet. On macOS 15+, use .dismissalConfirmationDialog("Discard?", shouldPresent: hasUnsavedChanges) for window dismissal confirmation. Route every programmatic close through the same save/validate/discard gate; interactiveDismissDisabled guards interactive dismissal only.

Enum-driven sheet routing: Define a SheetDestination enum that is Identifiable, store it on the router, and map it with a shared view modifier. This lets any child view present sheets without prop-drilling. See references/sheets.md [blocked] for the full centralized sheet routing pattern.

Tab-Based Navigation

Use the Tab API with a selection binding for scalable tab architecture. Each tab should wrap its content in an independent NavigationStack.

swift
struct MainTabView: View {    @State private var selectedTab: AppTab = .home
    var body: some View {        TabView(selection: $selectedTab) {            Tab("Home", systemImage: "house", value: .home) {                NavigationStack { HomeView() }            }            Tab("Search", systemImage: "magnifyingglass", value: .search) {                NavigationStack { SearchView() }            }            Tab("Profile", systemImage: "person", value: .profile) {                NavigationStack { ProfileView() }            }        }    }}

Custom binding with side effects: Route selection changes through a function to intercept special tabs (e.g., compose) that should trigger an action instead of changing selection.

iOS 26 Tab Additions

  • Tab(value:role:) with .search -- marks a dedicated search tab with system default search title, icon, and pinning behavior
  • .tabViewSearchActivation(_:) -- controls search tab activation and deactivation behavior
  • .tabBarMinimizeBehavior(_:) -- .onScrollDown, .onScrollUp, .never (iPhone only)
  • .tabViewSidebarHeader/Footer -- customize sidebar sections on iPadOS/macOS
  • .tabViewBottomAccessory { } -- attach content below the tab bar (e.g., Now Playing bar)
  • TabSection -- group tabs into sidebar sections with .tabPlacement(.sidebarOnly)

See references/tabview.md [blocked] for full TabView patterns including custom bindings, dynamic tabs, and sidebar customization.

Deep Links

Use parse → validate → commit. Parse into a typed route without mutating navigation; validate scheme/host/path, identifier shape, authorization, and destination existence; then update tab/path atomically. Invalid links must leave the current navigation unchanged.

Universal Links

Universal links let iOS open your app for standard HTTPS URLs. They require:

  1. An Apple App Site Association (AASA) file at /.well-known/apple-app-site-association
  2. An Associated Domains entitlement (applinks:example.com)

Handle Universal Links and custom URL schemes in SwiftUI with .onOpenURL:

swift
@mainstruct MyApp: App {    @State private var router = Router()
    var body: some Scene {        WindowGroup {            ContentView()                .environment(router)                .onOpenURL { url in router.handle(url: url) }        }    }}

Custom URL Schemes

Register schemes in Info.plist under CFBundleURLTypes. Handle with .onOpenURL. Prefer universal links over custom schemes for publicly shared links -- they provide web fallback and domain verification.

Handoff (NSUserActivity)

Advertise activities with .userActivity() and receive Handoff or other user activities with .onContinueUserActivity(). Declare activity types in Info.plist under NSUserActivityTypes. Set isEligibleForHandoff = true and provide a webpageURL as fallback.

See references/deeplinks.md [blocked] for full examples of AASA configuration, router URL handling, custom URL schemes, and NSUserActivity continuation.

Common Mistakes

  1. Using deprecated NavigationView -- use NavigationStack or NavigationSplitView
  2. Sharing one navigation path or router across all tabs -- each tab needs its own path
  3. Using .sheet(isPresented:) when state represents a model -- use .sheet(item:) instead
  4. Storing view instances in navigation paths -- store lightweight Hashable route data
  5. Nesting @Observable router objects inside other @Observable objects
  6. Prefer Tab(value:) with TabView(selection:) over the older .tabItem { } API
  7. Assuming tabBarMinimizeBehavior works on iPad -- it is iPhone only
  8. Handling deep links in multiple places -- centralize URL parsing in the router
  9. Hard-coding sheet frame dimensions -- use .presentationSizing(.form) instead
  10. Missing @MainActor on router classes -- required for Swift 6 concurrency safety

Review Checklist

  • NavigationStack used (not NavigationView)
  • Each tab has its own NavigationStack with independent path
  • Route enum is Hashable with stable identifiers
  • .navigationDestination(for:) maps all route types
  • .sheet(item:) preferred over .sheet(isPresented:)
  • Sheets own their dismiss logic internally
  • Router object is @MainActor and @Observable
  • Deep link URLs parsed and validated before navigation
  • Universal links have AASA and Associated Domains configured
  • Tab selection uses Tab(value:) with binding

References

  • NavigationStack and router patterns: references/navigationstack.md [blocked]
  • Sheet presentation and routing: references/sheets.md [blocked]
  • TabView patterns and iOS 26 API: references/tabview.md [blocked]
  • Deep links, universal links, and Handoff: references/deeplinks.md [blocked]
  • Architecture and state management: see swiftui-patterns skill
  • Layout and components: see swiftui-layout-components skill

来源与署名

来源:dpearson2699/swift-ios-skills位于skills/swiftui-navigation提交8d90fd1

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架

更多来自 dpearson2699/swift-ios-skills 的技能

Widgetkit

dpearson2699

指导实现、审查和改进 iOS、iPadOS、watchOS 与 CarPlay 上的 WidgetKit 小组件和控制项。

Software Development1.1K2个月前更新

Weatherkit

dpearson2699

指导 iOS 开发者使用 WeatherService 获取 WeatherKit 预报、警报与署名信息。

Software Development1.1K2个月前更新

Vision Framework

dpearson2699

Implement computer vision features including text recognition (OCR), face detection, barcode scanning, image segmentation, object tracking, and document scanning in iOS apps. Covers both the modern Swift-native Vision API (iOS 18+) and legacy VNRequest patterns, VisionKit DataScannerViewController for live camera scanning, and CoreMLRequest/VNCoreMLRequest for custom model inference. Use when adding OCR, barcode scanning, face detection, or custom Core ML model inference with Vision.

待分类1.1K2个月前更新

Tipkit

dpearson2699

Implement and review Apple TipKit feature-discovery UI for iOS 17+ apps. Use when adding or auditing in-app tips, contextual help, coach marks, Tip, TipView, popoverTip, rules, events, actions, display frequency, testing overrides, reusable tip identifiers, or iOS 18+ TipGroup and CloudKit tip sync; avoid for generic SwiftUI navigation or layout outside tip presentation.

待分类1.1K2个月前更新

Tabletopkit

dpearson2699

指导使用 TabletopKit 在 visionOS 上构建多人空间桌游,涵盖棋具、座位、动作与 RealityKit 渲染。

Software Development1.1K2个月前更新

Swiftui Webkit

dpearson2699

指导在 iOS 26 及更高版本的 SwiftUI 应用中使用 WebKit for SwiftUI 嵌入和控制网页内容。

Software Development1.1K2个月前更新