skip to content
Law

SwiftUI: How to use NavigationStack inside the .toolbar

/ 1 min read

First, why do I mostly find a solution after posting a Stackoverflow question?

So most examples and tutorials only use NavigationStack and NavigationLink inside a List. I’m surprised by how nobody seems to implement using the .toolbar modifier.

The solution was a little straightforward I found out.

Instead of NavigationView, you use the new and shiny NavigationStack. And use .navigationDestination() instead of NavigationLink.

@State private var goToSettings = false
NavigationStack {
ZStack {
// Some more codes
}
.toolbar {
Button(role: .destructive, action: {
goToSettings = true
}) {
Label("Settings", systemImage: "gearshape.fill").foregroundColor(colorForeground)
}
}
.navigationDestination(isPresented: $goToSettings, destination: {
SettingsView()
})
}

I need to get more familiar with SwiftUI’s modifiers as it is still a little confusing for me.