Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Created March 29, 2023 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonjs/630adbc45cfae8eebf80f988dda9da38 to your computer and use it in GitHub Desktop.
Save samsonjs/630adbc45cfae8eebf80f988dda9da38 to your computer and use it in GitHub Desktop.
Proof-of-concept for swapping out items in a navigation path in SwiftUI, see https://mastodon.social/@caseyliss/110103718721678762
import SwiftUI
enum NavDest: Hashable {
case a
case b
case c
case d(Int)
}
struct ContentView: View {
@State var navPath: [NavDest] = []
var body: some View {
NavigationStack(path: $navPath) {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
.padding(.bottom)
Button {
navPath.append(.a)
} label: {
Label("Push A", systemImage: "a.circle")
}
}
.padding()
.navigationDestination(for: NavDest.self) { dest in
switch dest {
case .a:
Button {
navPath.append(.b)
} label: {
Label("Push B", systemImage: "b.circle")
}
.buttonStyle(.borderedProminent)
.navigationTitle("A")
case .b:
Button {
navPath.append(.c)
} label: {
Label("Push C", systemImage: "c.circle")
}
.buttonStyle(.borderedProminent)
.navigationTitle("B")
case .c:
Button {
navPath.append(.d(1))
} label: {
Label("Push D(1)", systemImage: "d.circle")
}
.buttonStyle(.borderedProminent)
.navigationTitle("C")
case let .d(n):
HStack {
Button {
navPath[navPath.count - 1] = .d(n - 1)
} label: {
Label("Swap in D(\(n - 1))", systemImage: "arrow.down.circle")
}
.buttonStyle(.borderedProminent)
Button {
navPath[navPath.count - 1] = .d(n + 1)
} label: {
Label("Swap in D(\(n + 1))", systemImage: "arrow.up.circle")
}
.buttonStyle(.borderedProminent)
}
.navigationTitle("D(\(n))")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.buttonStyle(.borderedProminent)
}
}
import SwiftUI
@main
struct NavigationPathStuffApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.buttonStyle(.borderedProminent)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment