Created
March 29, 2023 01:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment