Axiom swiftui nav diag

From ComeOnOliver/skillshub · View interactive version

Axiom swiftui nav diag is an AI agent skill from the GitHub repository ComeOnOliver/skillshub. Use when debugging navigation not responding, unexpected pops, deep links showing wrong screen, state lost on tab switch or background, crashes in navigationDestination, or any SwiftUI navigation failure - systematic diagnostics with production crisis defense

How to use this skill

Skill file

SwiftUI Navigation Diagnostics

Overview

Core principle 85% of navigation problems stem from path state management errors, view identity issues, or placement mistakesβ€”not SwiftUI defects.

SwiftUI's navigation system is used by millions of apps and handles complex navigation patterns reliably. If your navigation is failing, not responding, or behaving unexpectedly, the issue is almost always in how you're managing navigation state, not the framework itself.

This skill provides systematic diagnostics to identify root causes in minutes, not hours.

Red Flags β€” Suspect Navigation Issue

If you see ANY of these, suspect a code issue, not framework breakage:

Critical distinction NavigationStack behavior is deterministic. If it's not working, you're modifying state incorrectly, have view identity issues, or navigationDestination is misplaced.

Mandatory First Steps

ALWAYS run these checks FIRST (before changing code):

// 1. Add NavigationPath logging
NavigationStack(path: $path) {
    RootView()
        .onChange(of: path.count) { oldCount, newCount in
            print("πŸ“ Path changed: \(oldCount) β†’ \(newCount)")
            // If this never fires, link isn't modifying path
            // If it fires unexpectedly, something else modifies path
        }
}

// 2. Check navigationDestination is visible
// Put temporary print in destination closure
.navigationDestination(for: Recipe.self) { recipe in
    let _ = print("πŸ”— Destination for Recipe: \(recipe.name)")
    RecipeDetail(recipe: recipe)
}
// If this never prints, destination isn't being evaluated

// 3. Check NavigationLink is inside NavigationStack
// Visual inspection: Trace from NavigationLink up view hierarchy
// Must hit NavigationStack, not another container first

// 4. Check path state location
// @State must be in stable view (not recreated each render)
// Must be @State, @StateObject, or @Observable β€” not local variable

// 5. Test basic case in isolation
// Create minimal reproduction
NavigationStack {
    NavigationLink("Test", value: "test")
        .navigationDestination(for: String.self) { str in
            Text("Pushed: \(str)")
        }
}
// If this works, problem is in your specific setup
What this tells you

| Observation | Diagnosis | Next Step |

|-------------|-----------|-----------|

| onChange never fires on tap | NavigationLink not in NavigationStack hierarchy | Pattern 1a |

| onChange fires but view doesn't push | navigationDestination not found/loaded | Pattern 1b |

| onChange fires, view pushes, then immediate pop | View identity issue or path modification | Pattern 2a |

| Path changes unexpectedly (not from tap) | External code modifying path | Pattern 2b |

| Deep link path.append() doesn't navigate | Timing issue or wrong thread | Pattern 3b |

| State lost on tab switch | NavigationStack shared across tabs | Pattern 4a |

| Works first time, fails on return | View recreation issue | Pattern 5a |

MANDATORY INTERPRETATION

Before changing ANY code, identify ONE of these:

  1. If link tap does nothing AND no onChange β†’ Link outside NavigationStack (check hierarchy)
  2. If onChange fires but nothing pushes β†’ navigationDestination not in scope (check placement)
  3. If pushes then immediately pops β†’ View identity change or path reset (check @State location)
  4. If deep link fails β†’ Timing or MainActor issue (check thread)
  5. If crash β†’ Force unwrap on path decode or missing type registration
If diagnostics are contradictory or unclear

Decision Tree

Use this to reach the correct diagnostic pattern in 2 minutes:

Navigation problem?
β”œβ”€ Navigation tap does nothing?
β”‚  β”œβ”€ NavigationLink inside NavigationStack?
β”‚  β”‚  β”œβ”€ No β†’ Pattern 1a (Link outside Stack)
β”‚  β”‚  └─ Yes β†’ Check navigationDestination
β”‚  β”‚
β”‚  β”œβ”€ navigationDestination registered?
β”‚  β”‚  β”œβ”€ Inside lazy container? β†’ Pattern 1b (Lazy Loading)
β”‚  β”‚  β”œβ”€ Type mismatch? β†’ Pattern 1c (Type Registration)
β”‚  β”‚  └─ Blocked by sheet/popover? β†’ Pattern 1d (Modal Blocking)
β”‚  β”‚
β”‚  └─ Using view-based link?
β”‚     └─ β†’ Pattern 1e (Deprecated API)
β”‚
β”œβ”€ Unexpected pop back?
β”‚  β”œβ”€ Immediate pop after push?
β”‚  β”‚  β”œβ”€ View body recreating path? β†’ Pattern 2a (Path Recreation)
β”‚  β”‚  β”œβ”€ @State in wrong view? β†’ Pattern 2a (State Location)
β”‚  β”‚  └─ ForEach id changing? β†’ Pattern 2c (Identity Change)
β”‚  β”‚
β”‚  β”œβ”€ Pop when shouldn't?
β”‚  β”‚  β”œβ”€ External code calling removeLast? β†’ Pattern 2b (Unexpected Modification)
β”‚  β”‚  β”œβ”€ Task cancelled? β†’ Pattern 2b (Async Cancellation)
β”‚  β”‚  └─ MainActor issue? β†’ Pattern 2d (Threading)
β”‚  β”‚
β”‚  └─ Back button behavior wrong?
β”‚     └─ β†’ Pattern 2e (Stack Corruption)
β”‚
β”œβ”€ Deep link not working?
β”‚  β”œβ”€ URL not received?
β”‚  β”‚  β”œβ”€ onOpenURL not called? β†’ Check URL scheme in Info.plist
β”‚  β”‚  └─ Universal Links issue? β†’ Check apple-app-site-association
β”‚  β”‚
β”‚  β”œβ”€ URL received, path not updated?
β”‚  β”‚  β”œβ”€ path.append not on MainActor? β†’ Pattern 3a (Threading)
β”‚  β”‚  β”œβ”€ Timing issue (app not ready)? β†’ Pattern 3b (Initialization)
β”‚  β”‚  └─ NavigationStack not created yet? β†’ Pattern 3b (Lifecycle)
β”‚  β”‚
β”‚  └─ Path updated, wrong screen shown?
β”‚     β”œβ”€ Wrong path order? β†’ Pattern 3c (Path Construction)
β”‚     β”œβ”€ Wrong type appended? β†’ Pattern 3c (Type Mismatch)
β”‚     └─ Item not found? β†’ Pattern 3d (Data Resolution)
β”‚
β”œβ”€ State lost?
β”‚  β”œβ”€ Lost on tab switch?
β”‚  β”‚  β”œβ”€ Shared NavigationStack? β†’ Pattern 4a (Shared State)
β”‚  β”‚  └─ Tab recreation? β†’ Pattern 4a (Tab Identity)
β”‚  β”‚
β”‚  β”œβ”€ Lost on background/foreground?
β”‚  β”‚  β”œβ”€ No SceneStorage? β†’ Pattern 4b (No Persistence)
β”‚  β”‚  └─ Decode failure? β†’ Pattern 4c (Decode Error)
β”‚  β”‚
β”‚  └─ Lost on rotation/size change?
β”‚     └─ β†’ Pattern 4d (Layout Recreation)
β”‚
β”œβ”€ NavigationSplitView issue?
β”‚  β”œβ”€ Sidebar not visible on iPad?
β”‚  β”‚  β”œβ”€ columnVisibility not set? β†’ Pattern 6a (Column Visibility)
β”‚  β”‚  └─ Compact size class? β†’ Pattern 6a (Automatic Adaptation)
β”‚  β”‚
β”‚  β”œβ”€ Detail shows blank on iPad?
β”‚  β”‚  β”œβ”€ No default detail view? β†’ Pattern 6b (Missing Detail)
β”‚  β”‚  └─ Selection binding nil? β†’ Pattern 6b (Selection State)
β”‚  β”‚
β”‚  └─ Works on iPhone, broken on iPad?
β”‚     └─ β†’ Pattern 6c (Platform Adaptation)
β”‚
└─ Crash?
   β”œβ”€ EXC_BAD_ACCESS in navigation code?
   β”‚  └─ β†’ Pattern 5a (Memory Issue)
   β”‚
   β”œβ”€ Fatal error: type not registered?
   β”‚  └─ β†’ Pattern 5b (Missing Destination)
   β”‚
   └─ Decode failure on restore?
      └─ β†’ Pattern 5c (Restoration Crash)

Pattern Selection Rules (MANDATORY)

Before proceeding to a pattern:

  1. Navigation tap does nothing β†’ Add onChange logging FIRST, then Pattern 1
  2. Unexpected pop β†’ Find WHAT is modifying path (logging), then Pattern 2
  3. Deep link fails β†’ Verify URL received (print in onOpenURL), then Pattern 3
  4. State lost β†’ Identify WHEN lost (tab switch vs background), then Pattern 4
  5. Crash β†’ Get full stack trace, then Pattern 5
Apply ONE pattern at a time
FORBIDDEN

Diagnostic Patterns

Pattern 1a: NavigationLink Outside NavigationStack

Time cost 5-10 minutes

Symptom
Diagnosis
// Check view hierarchy β€” NavigationLink must be INSIDE NavigationStack

// ❌ WRONG β€” Link outside stack
struct ContentView: View {
    var body: some View {
        VStack {
            NavigationLink("Go", value: "test")  // Outside stack!
            NavigationStack {
                Text("Root")
            }
        }
    }
}

// Check: Add background color to NavigationStack
NavigationStack {
    Color.red  // If link is on red, it's inside
}
Fix
// βœ… CORRECT β€” Link inside stack
struct ContentView: View {
    var body: some View {
        NavigationStack {
            VStack {
                NavigationLink("Go", value: "test")  // Inside stack
                Text("Root")
            }
            .navigationDestination(for: String.self) { str in
                Text("Pushed: \(str)")
            }
        }
    }
}
Verification

Pattern 1b: navigationDestination in Lazy Container

Time cost 10-15 minutes

Symptom
Diagnosis
// ❌ WRONG β€” Destination inside lazy container (may not be loaded)
ScrollView {
    LazyVStack {
        ForEach(items) { item in
            NavigationLink(item.name, value: item)
                .navigationDestination(for: Item.self) { item in
                    ItemDetail(item: item)  // May not be evaluated!
                }
        }
    }
}
Fix
// βœ… CORRECT β€” Destination outside lazy container
ScrollView {
    LazyVStack {
        ForEach(items) { item in
            NavigationLink(item.name, value: item)
        }
    }
}
.navigationDestination(for: Item.self) { item in
    ItemDetail(item: item)  // Always available
}
Verification

Pattern 1c: Type Registration Mismatch

Time cost 10 minutes

Symptom
Diagnosis
// Check: Value type must EXACTLY match destination type

// Link uses Recipe
NavigationLink(recipe.name, value: recipe)  // value is Recipe

// Destination registered for... Recipe.ID?
.navigationDestination(for: Recipe.ID.self) { id in  // ❌ Wrong type!
    RecipeDetail(id: id)
}
Fix
// Match types exactly
NavigationLink(recipe.name, value: recipe)  // Recipe

.navigationDestination(for: Recipe.self) { recipe in  // βœ… Recipe
    RecipeDetail(recipe: recipe)
}

// OR change link to use ID
NavigationLink(recipe.name, value: recipe.id)  // Recipe.ID

.navigationDestination(for: Recipe.ID.self) { id in  // βœ… Recipe.ID
    RecipeDetail(id: id)
}
Verification

Pattern 2a: NavigationPath Recreated Every Render

Time cost 15-20 minutes

Symptom
Diagnosis
// ❌ WRONG β€” Path created in view body (reset every render)
struct ContentView: View {
    var body: some View {
        let path = NavigationPath()  // Recreated every time!
        NavigationStack(path: .constant(path)) {
            // ...
        }
    }
}

// ❌ WRONG β€” @State in child view that gets recreated
struct ParentView: View {
    @State var showChild = true
    var body: some View {
        if showChild {
            ChildView()  // Recreated when showChild toggles
        }
    }
}

struct ChildView: View {
    @State var path = NavigationPath()  // Lost when ChildView recreated
    // ...
}
Fix
// βœ… CORRECT β€” @State at stable level
struct ContentView: View {
    @State private var path = NavigationPath()  // Persists across renders

    var body: some View {
        NavigationStack(path: $path) {
            RootView()
        }
    }
}

// βœ… CORRECT β€” @StateObject for ObservableObject
struct ContentView: View {
    @StateObject private var navModel = NavigationModel()

    var body: some View {
        NavigationStack(path: $navModel.path) {
            RootView()
        }
    }
}
Verification

Pattern 2d: Path Modified Off MainActor

Time cost 10-15 minutes

Symptom
Diagnosis
// ❌ WRONG β€” Modifying path from background task
func loadAndNavigate() async {
    let recipe = await fetchRecipe()
    path.append(recipe)  // ⚠️ Not on MainActor!
}

// Check: Search for path.append, path.removeLast outside @MainActor context
Fix
// βœ… CORRECT β€” Ensure MainActor
@MainActor
func loadAndNavigate() async {
    let recipe = await fetchRecipe()
    path.append(recipe)  // βœ… MainActor isolated
}

// OR explicitly dispatch
func loadAndNavigate() async {
    let recipe = await fetchRecipe()
    await MainActor.run {
        path.append(recipe)
    }
}

// βœ… BEST β€” Use @Observable with @MainActor
@Observable
@MainActor
class Router {
    var path = NavigationPath()

    func navigate(to value: any Hashable) {
        path.append(value)
    }
}
Verification

Pattern 3a: Deep Link Threading Issue

Time cost 15-20 minutes

Symptom
Diagnosis
// ❌ WRONG β€” May be called before NavigationStack exists
.onOpenURL { url in
    handleDeepLink(url)  // NavigationStack may not be rendered yet
}

func handleDeepLink(_ url: URL) {
    path.append(parsedValue)  // Modifies path that doesn't exist yet
}
Fix
// βœ… CORRECT β€” Defer deep link handling
@State private var pendingDeepLink: URL?
@State private var isReady = false

var body: some View {
    NavigationStack(path: $path) {
        RootView()
            .onAppear {
                isReady = true
                if let url = pendingDeepLink {
                    handleDeepLink(url)
                    pendingDeepLink = nil
                }
            }
    }
    .onOpenURL { url in
        if isReady {
            handleDeepLink(url)
        } else {
            pendingDeepLink = url  // Queue for later
        }
    }
}
Verification

Pattern 3c: Deep Link Path Construction Order

Time cost 10-15 minutes

Symptom
Diagnosis
// ❌ WRONG β€” Wrong order (child before parent)
// URL: myapp://category/desserts/recipe/apple-pie
func handleDeepLink(_ url: URL) {
    path.append(recipe)    // Recipe pushed first
    path.append(category)  // Category pushed second β€” WRONG ORDER
}
// User sees Category screen, not Recipe screen
Fix
// βœ… CORRECT β€” Parent before child
func handleDeepLink(_ url: URL) {
    path.removeLast(path.count)  // Clear existing

    // Build hierarchy: parent β†’ child
    path.append(category)  // First: Category
    path.append(recipe)    // Second: Recipe (shows this screen)
}

// For complex paths, build array first
var newPath: [any Hashable] = []
// Parse URL segments...
newPath.append(category)
newPath.append(subcategory)
newPath.append(item)

// Then apply
path = NavigationPath(newPath)
Verification

Pattern 4a: Shared NavigationStack Across Tabs

Time cost 15-20 minutes

Symptom
Diagnosis
// ❌ WRONG β€” Single NavigationStack wrapping TabView
NavigationStack(path: $path) {
    TabView {
        Tab("Home") { HomeView() }
        Tab("Settings") { SettingsView() }
    }
}
// All tabs share same navigation β€” state mixed/lost

// ❌ WRONG β€” Same @State used across tabs
@State var path = NavigationPath()  // Shared
TabView {
    Tab("Home") {
        NavigationStack(path: $path) { ... }  // Uses shared path
    }
    Tab("Settings") {
        NavigationStack(path: $path) { ... }  // Same path!
    }
}
Fix
// βœ… CORRECT β€” Each tab has own NavigationStack
TabView {
    Tab("Home", systemImage: "house") {
        NavigationStack {  // Own stack
            HomeView()
                .navigationDestination(for: HomeItem.self) { ... }
        }
    }
    Tab("Settings", systemImage: "gear") {
        NavigationStack {  // Own stack
            SettingsView()
                .navigationDestination(for: SettingItem.self) { ... }
        }
    }
}

// For per-tab path tracking:
struct HomeTab: View {
    @State private var path = NavigationPath()  // Tab-specific

    var body: some View {
        NavigationStack(path: $path) {
            HomeView()
        }
    }
}
Verification

Pattern 4b: No State Persistence on Background

Time cost 15-20 minutes

Symptom
Diagnosis
// ❌ WRONG β€” No persistence mechanism
@State private var path = NavigationPath()
// Path lost when app terminates
Fix
// βœ… CORRECT β€” Use SceneStorage + Codable
struct ContentView: View {
    @StateObject private var navModel = NavigationModel()
    @SceneStorage("navigation") private var savedData: Data?

    var body: some View {
        NavigationStack(path: $navModel.path) {
            RootView()
        }
        .task {
            // Restore on appear
            if let data = savedData {
                navModel.restore(from: data)
            }
            // Save on changes
            for await _ in navModel.objectWillChange.values {
                savedData = navModel.encoded()
            }
        }
    }
}

@MainActor
class NavigationModel: ObservableObject {
    @Published var path = NavigationPath()

    func encoded() -> Data? {
        guard let codable = path.codable else { return nil }
        return try? JSONEncoder().encode(codable)
    }

    func restore(from data: Data) {
        guard let codable = try? JSONDecoder().decode(
            NavigationPath.CodableRepresentation.self,
            from: data
        ) else { return }
        path = NavigationPath(codable)
    }
}
Verification

Pattern 5b: Missing navigationDestination Registration

Time cost 10-15 minutes

Symptom
Diagnosis
// Every type pushed on path needs a destination

// You push Recipe
path.append(recipe)  // Recipe type

// But only registered Category
.navigationDestination(for: Category.self) { ... }
// No destination for Recipe!
Fix
// Register ALL types you might push
NavigationStack(path: $path) {
    RootView()
        .navigationDestination(for: Category.self) { category in
            CategoryView(category: category)
        }
        .navigationDestination(for: Recipe.self) { recipe in
            RecipeDetail(recipe: recipe)
        }
        .navigationDestination(for: Chef.self) { chef in
            ChefProfile(chef: chef)
        }
}

// Or use enum route type for single registration
enum AppRoute: Hashable {
    case category(Category)
    case recipe(Recipe)
    case chef(Chef)
}

.navigationDestination(for: AppRoute.self) { route in
    switch route {
    case .category(let cat): CategoryView(category: cat)
    case .recipe(let recipe): RecipeDetail(recipe: recipe)
    case .chef(let chef): ChefProfile(chef: chef)
    }
}
Verification

Pattern 5c: State Restoration Decode Crash

Time cost 15-20 minutes

Symptom
Diagnosis
// ❌ WRONG β€” Force unwrap decode
func restore(from data: Data) {
    let codable = try! JSONDecoder().decode(  // πŸ’₯ Crashes!
        NavigationPath.CodableRepresentation.self,
        from: data
    )
    path = NavigationPath(codable)
}

// Crash reasons:
// - Saved path contains type that no longer exists
// - Codable encoding changed between versions
// - Saved item was deleted
Fix
// βœ… CORRECT β€” Graceful decode with fallback
func restore(from data: Data) {
    do {
        let codable = try JSONDecoder().decode(
            NavigationPath.CodableRepresentation.self,
            from: data
        )
        path = NavigationPath(codable)
    } catch {
        print("Navigation restore failed: \(error)")
        path = NavigationPath()  // Start fresh
        // Optionally clear bad saved data
    }
}

// βœ… BETTER β€” Store IDs, resolve to objects
class NavigationModel: ObservableObject, Codable {
    var selectedIds: [String] = []  // Store IDs

    func resolvedPath(dataModel: DataModel) -> NavigationPath {
        var path = NavigationPath()
        for id in selectedIds {
            if let item = dataModel.item(withId: id) {
                path.append(item)
            }
            // Missing items silently skipped
        }
        return path
    }
}
Verification

Production Crisis Scenario

Context: Navigation Randomly Breaks After iOS Update

Situation
Pressure signals
Rationalization traps (DO NOT fall into these)
  1. "It's an iOS 18 bug, wait for Apple to fix"
  1. "Let's wrap UINavigationController"
  1. "Add retry logic for navigation"
  1. "Roll back to pre-iOS 18 version"
MANDATORY Diagnostic Protocol

You have 2 hours to provide CTO with:

  1. Root cause
  2. Fix timeline
  3. Workaround for affected users
Step 1: Identify Pattern (30 minutes)
// Release build with diagnostic logging
#if DEBUG || DIAGNOSTIC
NavigationStack(path: $path) {
    // ...
}
.onChange(of: path.count) { old, new in
    Analytics.log("nav_path_change", ["old": old, "new": new])
}
#endif

// Check analytics for:
// - path.count going to 0 unexpectedly β†’ Path recreation
// - path.count increasing but no push β†’ Missing destination
// - No path changes at all β†’ Link not firing
Step 2: Cross-Reference with iOS 18 Changes (15 minutes)
// iOS 18 changes that affect navigation:
// 1. Stricter MainActor enforcement
// 2. Changes to view identity in TabView
// 3. New navigation lifecycle timing

// Most common iOS 18 issue:
// Code that worked by accident now fails

// Check: Any path modifications in async contexts without @MainActor?
Task {
    let result = await fetch()
    path.append(result)  // ⚠️ iOS 18 stricter about this
}
Step 3: Apply Targeted Fix (30 minutes)
// Root cause found: NavigationPath modified from async context
// iOS 17 was lenient, iOS 18 enforces MainActor properly

// ❌ Old code (worked on iOS 17, breaks on iOS 18)
func loadAndNavigate() async {
    let recipe = await fetchRecipe()
    path.append(recipe)  // Race condition
}

// βœ… Fix: Explicit MainActor isolation
@MainActor
func loadAndNavigate() async {
    let recipe = await fetchRecipe()
    path.append(recipe)  // βœ… Safe
}

// OR: Annotate entire class
@Observable
@MainActor
class Router {
    var path = NavigationPath()

    func navigate(to value: any Hashable) {
        path.append(value)
    }
}
Step 4: Validate and Deploy (45 minutes)
// 1. Test on iOS 17 device β€” still works
// 2. Test on iOS 18 device β€” now works
// 3. Test all navigation paths
// 4. Submit expedited review

// Expedited review justification:
// "Critical bug fix for iOS 18 compatibility affecting 20% of users"
Professional Communication Templates
To CTO (45 minutes after starting)
Root cause identified: Navigation code wasn't properly isolated
to the main thread. iOS 18 enforces this more strictly than iOS 17.

Fix: Add @MainActor annotation to navigation code.
Already tested on iOS 17 (no regression) and iOS 18 (fixes issue).

Timeline:
- Fix ready: Now
- QA validation: 1 hour
- App Store submission: Today
- Available to users: 24-48 hours (expedited review)

Workaround for affected users: Force quit and relaunch app
often clears the issue temporarily.
To Engineering Team
iOS 18 Navigation Fix

Root cause: NavigationPath modifications in async contexts
without @MainActor isolation. iOS 17 was permissive, iOS 18 enforces.

Fix applied:
- Added @MainActor to Router class
- Updated all path.append/removeLast calls to be MainActor-isolated
- Added Swift 6 concurrency checking to catch future issues

Files changed: Router.swift, ContentView.swift, DeepLinkHandler.swift

Testing needed:
- All navigation flows
- Deep links from cold start
- Tab switching with navigation state
- Background/foreground with navigation state

Pattern 6: NavigationSplitView Platform Issues

Time cost 10-20 minutes

NavigationSplitView adapts automatically between compact (iPhone) and regular (iPad) size classes. Most issues arise because developers test only on iPhone, where it collapses to a NavigationStack.

Pattern 6a: Sidebar/Column Not Visible

Symptom
Diagnosis
// Check 1: Is columnVisibility controlling visibility?
@State private var columnVisibility: NavigationSplitViewVisibility = .all

NavigationSplitView(columnVisibility: $columnVisibility) {
    // sidebar
} detail: {
    // detail
}

// Check 2: Are you in compact size class? (iPhone or iPad slide-over)
// In compact, NavigationSplitView collapses to NavigationStack automatically
// The sidebar becomes the root of the stack
Fix

Pattern 6b: Blank Detail View on iPad

Symptom
Fix β€” Provide Default Detail
NavigationSplitView {
    List(items, selection: $selectedItem) { item in
        Text(item.name)
    }
} detail: {
    if let selectedItem {
        ItemDetailView(item: selectedItem)
    } else {
        ContentUnavailableView("Select an Item",
            systemImage: "sidebar.left",
            description: Text("Choose an item from the sidebar"))
    }
}

Key insight: iPad shows the detail column immediately on launch. Without a default view, it's blank. iPhone doesn't show this because it starts on the sidebar.

Pattern 6c: Platform Adaptation Issues

Symptom
Diagnosis

NavigationSplitView uses different navigation models per size class:

// Common mistake: using NavigationLink inside NavigationSplitView sidebar
// This creates DOUBLE navigation on iPad (link push + selection)
// Fix: Use List(selection:) binding, not NavigationLink
NavigationSplitView {
    List(items, selection: $selectedID) { item in  // βœ… selection binding
        Text(item.name)
    }
} detail: {
    // driven by selectedID
}

Test on both iPhone AND iPad before shipping. Most NavigationSplitView bugs are platform-specific.


Quick Reference Table

| Symptom | Likely Cause | First Check | Pattern | Fix Time |

|---------|--------------|-------------|---------|----------|

| Link tap does nothing | Link outside stack | View hierarchy | 1a | 5-10 min |

| Intermittent navigation failure | Destination in lazy container | Destination placement | 1b | 10-15 min |

| Works for some types, not others | Type mismatch | Print type(of:) | 1c | 10 min |

| Push then immediate pop | Path recreated | @State location | 2a | 15-20 min |

| Random unexpected pops | External path modification | Add logging | 2b | 15-20 min |

| Works on MainActor, fails in Task | Threading issue | Check @MainActor | 2d | 10-15 min |

| Deep link doesn't navigate | Not on MainActor | Thread check | 3a | 15-20 min |

| Deep link from cold start fails | Timing/lifecycle | Add pendingDeepLink | 3b | 15-20 min |

| Deep link shows wrong screen | Path order wrong | Print path contents | 3c | 10-15 min |

| State lost on tab switch | Shared NavigationStack | Check Tab structure | 4a | 15-20 min |

| State lost on background | No persistence | Add SceneStorage | 4b | 20-25 min |

| Crash on launch (decode) | Force unwrap decode | Error handling | 5c | 15-20 min |

| "No destination found" crash | Missing registration | List all types | 5b | 10-15 min |

| Sidebar missing on iPad | columnVisibility | Check binding | 6a | 10-15 min |

| Blank detail on iPad | No default detail | Add ContentUnavailableView | 6b | 10 min |

| Works iPhone, broken iPad | Platform adaptation | Test both size classes | 6c | 15-20 min |


Common Mistakes

Mistake 1: Putting navigationDestination Inside ForEach

Problem Destination not loaded when needed (lazy evaluation).

Why it fails LazyVStack/ForEach don't evaluate all children. Destination may not exist when link is tapped.

Fix
// Move destination OUTSIDE lazy container
List {
    ForEach(items) { item in
        NavigationLink(item.name, value: item)
    }
}
.navigationDestination(for: Item.self) { item in
    ItemDetail(item: item)
}

Mistake 2: Using NavigationView on iOS 16+

Problem NavigationView deprecated, different behavior across versions.

Why it fails No NavigationPath support, can't programmatically navigate or deep link reliably.

Fix

Mistake 3: Creating NavigationPath in computed property

Problem Path reset every access.

Why it fails var body is called repeatedly. Creating path there means it's reset constantly.

Fix
// Use @State, not computed
@State private var path = NavigationPath()  // βœ… Persists

// NOT
var path: NavigationPath { NavigationPath() }  // ❌ Reset every time

Mistake 4: Not Handling Decode Errors in Restoration

Problem Crash when saved navigation data is invalid.

Why it fails Data model changes, items deleted, encoding format changes between app versions.

Fix

Mistake 5: Assuming Deep Links Work Immediately

Problem Deep link on cold start fails.

Why it fails onOpenURL may fire before NavigationStack is rendered.

Fix

Cross-References

For Preventive Patterns

swiftui-nav skill β€” Discipline-enforcing anti-patterns:

For API Reference

swiftui-nav-ref skill β€” Complete API documentation:

For Related Issues

swift-concurrency skill β€” If MainActor issues:


Last Updated 2025-12-05

Status Production-ready diagnostics

Tested Diagnostic patterns validated against common navigation issues

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.