Skip to content

Instantly share code, notes, and snippets.

@inamiy
Last active June 12, 2022 06:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inamiy/e59a80c56dbf82e6ad122d5da92435e3 to your computer and use it in GitHub Desktop.
Save inamiy/e59a80c56dbf82e6ad122d5da92435e3 to your computer and use it in GitHub Desktop.
iOS 16 NavigationStack binding + navigationDestination behavior (Xcode 14 Beta 1, Swift 5.7) https://twitter.com/inamiy/status/1535861181998862337
import SwiftUI
struct RootStringNavStack: View {
@State
var navigationPath: [String] = []
// For hook-printing.
var navigationPathBinding: Binding<[String]> {
.init(
get: { navigationPath },
set: {
print("===> navigationPath", $0)
navigationPath = $0
}
)
}
var body: some View {
NavigationStack(path: navigationPathBinding) {
StringNavStackChildView(
title: "String NavStack",
navigationPath: $navigationPath
)
// Top-level navigationDestination only.
.navigationDestination(for: String.self) { route in
// TODO: Why `navigationPath.count = 1` is printed after `N >= 2` push transition?
let _ = print("===> navigationDestination, route = \(route), navigationPath.count = \(navigationPath.count)")
StringNavStackChildView(title: route, navigationPath: $navigationPath)
}
}
}
}
struct StringNavStackChildView: View {
let title: String
@Binding
var navigationPath: [String]
init(title: String, navigationPath: Binding<[String]>) {
self.title = title
self._navigationPath = navigationPath
}
var body: some View {
List {
NavigationLink(value: "1") {
Text("Link 1")
}
NavigationLink(value: "2") {
Text("Link 2")
}
NavigationLink(value: "3") {
Text("Link 3")
}
}
.navigationTitle(title)
.toolbar {
Button(action: { navigationPath = [] }) {
Text("Go Home")
}
}
}
}
/*
// 1. Tap Link 1
===> navigationPath ["1"]
===> navigationDestination, route = 1, navigationPath.count = 1
===> navigationDestination, route = 1, navigationPath.count = 1
// 2. Tap Link 2
===> navigationPath ["1", "2"]
===> navigationDestination, route = 2, navigationPath.count = 2
===> navigationDestination, route = 1, navigationPath.count = 1
===> navigationDestination, route = 2, navigationPath.count = 1
// 3. Tap Link 3
===> navigationPath ["1", "2", "3"]
===> navigationDestination, route = 3, navigationPath.count = 3
===> navigationDestination, route = 1, navigationPath.count = 1
===> navigationDestination, route = 2, navigationPath.count = 1
===> navigationDestination, route = 3, navigationPath.count = 1
// 4. Back button
===> navigationPath ["1", "2"]
===> navigationDestination, route = 1, navigationPath.count = 2
===> navigationDestination, route = 2, navigationPath.count = 2
===> navigationDestination, route = 1, navigationPath.count = 1
===> navigationDestination, route = 2, navigationPath.count = 1
// 5. Back button
===> navigationPath ["1"]
===> navigationDestination, route = 1, navigationPath.count = 1
===> navigationDestination, route = 1, navigationPath.count = 1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment