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:
-
Navigation tap does nothing (link present but doesn't push)
-
Back button pops to wrong screen or root
-
Deep link opens app but shows wrong screen
-
Navigation state lost when switching tabs
-
Navigation state lost when app backgrounds
-
Same NavigationLink pushes twice
-
Navigation animation stuck or janky
-
Crash with navigationDestination in stack trace
-
β FORBIDDEN "SwiftUI navigation is broken, let's wrap UINavigationController"
- NavigationStack is used by Apple's own apps
- Wrapping UIKit adds complexity and loses SwiftUI state management benefits
- UIKit interop has its own edge cases you'll spend weeks discovering
- Your issue is almost certainly path management, not framework defect
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):
NavigationStack(path: $path) {
RootView()
.onChange(of: path.count) { oldCount, newCount in
print("π Path changed: \(oldCount) β \(newCount)")
}
}
.navigationDestination(for: Recipe.self) { recipe in
let _ = print("π Destination for Recipe: \(recipe.name)")
RecipeDetail(recipe: recipe)
}
NavigationStack {
NavigationLink("Test", value: "test")
.navigationDestination(for: String.self) { str in
Text("Pushed: \(str)")
}
}
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:
- If link tap does nothing AND no onChange β Link outside NavigationStack (check hierarchy)
- If onChange fires but nothing pushes β navigationDestination not in scope (check placement)
- If pushes then immediately pops β View identity change or path reset (check @State location)
- If deep link fails β Timing or MainActor issue (check thread)
- If crash β Force unwrap on path decode or missing type registration
If diagnostics are contradictory or unclear
- STOP. Do NOT proceed to patterns yet
- Add print statements at every path modification point
- Create minimal reproduction case
- Test with String values first (simplest case)
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:
- Navigation tap does nothing β Add onChange logging FIRST, then Pattern 1
- Unexpected pop β Find WHAT is modifying path (logging), then Pattern 2
- Deep link fails β Verify URL received (print in onOpenURL), then Pattern 3
- State lost β Identify WHEN lost (tab switch vs background), then Pattern 4
- Crash β Get full stack trace, then Pattern 5
Apply ONE pattern at a time
- Implement the fix from one pattern
- Test thoroughly
- Only if issue persists, try next pattern
- DO NOT apply multiple patterns simultaneously (can't isolate cause)
FORBIDDEN
- Guessing at solutions without diagnostics
- Changing multiple things at once
- Wrapping with UINavigationController "because SwiftUI is broken"
- Adding delays/DispatchQueue.main.async without understanding why
- Switching to view-based NavigationLink "to avoid path issues"
Diagnostic Patterns
Pattern 1a: NavigationLink Outside NavigationStack
Time cost 5-10 minutes
Symptom
- Tapping NavigationLink does nothing
- No navigation occurs, no errors
- onChange(of: path) never fires
Diagnosis
struct ContentView: View {
var body: some View {
VStack {
NavigationLink("Go", value: "test")
NavigationStack {
Text("Root")
}
}
}
}
NavigationStack {
Color.red
}
Fix
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
NavigationLink("Go", value: "test")
Text("Root")
}
.navigationDestination(for: String.self) { str in
Text("Pushed: \(str)")
}
}
}
}
Verification
- Tap link, navigation occurs
- onChange(of: path) fires when tapped
Pattern 1b: navigationDestination in Lazy Container
Time cost 10-15 minutes
Symptom
- NavigationLink tap does nothing OR works intermittently
- onChange fires (path updated) but view doesn't push
- Console may show: "A navigationDestination for [Type] was not found"
Diagnosis
ScrollView {
LazyVStack {