Skip to content

Instantly share code, notes, and snippets.

@MatthewWaller
Last active January 11, 2023 05:03
Show Gist options
  • Save MatthewWaller/3a2f8faee9165afa978e8e9353df028b to your computer and use it in GitHub Desktop.
Save MatthewWaller/3a2f8faee9165afa978e8e9353df028b to your computer and use it in GitHub Desktop.
An Example of NavigationStack Navigation
import SwiftUI
enum NavigationPath {
case red
case blue
case green
}
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State var path: [NavigationPath] = []
var body: some View {
NavigationStack {
RedView()
.navigationDestination(for: NavigationPath.self) { value in
switch value {
case .red:
RedView()
case .blue:
BlueView()
case .green:
GreenView()
}
}
}
}
}
struct RedView: View {
var body: some View {
VStack {
NavigationLink(value: NavigationPath.blue) {
Text("Blue")
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.navigationTitle(Text("Red View"))
.background(Color.red)
}
}
struct BlueView: View {
var body: some View {
VStack {
NavigationLink(value: NavigationPath.green) {
Text("Green")
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.navigationTitle(Text("Blue View"))
.background(Color.blue)
}
}
struct GreenView: View {
var body: some View {
VStack {
NavigationLink(value: NavigationPath.red) {
Text("Red")
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.navigationTitle(Text("Green View"))
.background(Color.green)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment