Creating a navigation stack in SwiftUI is a fundamental part of building iOS apps. SwiftUI provides a declarative way to manage navigation using the NavigationStack (introduced in iOS 16) or NavigationView (for older versions). Below is a detailed tutorial on how to create a navigation stack in SwiftUI.
import SwiftUI
struct NavigationStackExample1: View {
var body: some View {
NavigationStack {
VStack {
Text("Home Screen")
.font(.largeTitle)
.padding()
NavigationLink("Go to Detail View") {
DetailView()
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.navigationTitle("Home")
}
}
}
private struct DetailView: View {
var body: some View {
VStack {
Text("Detail Screen")
.font(.largeTitle)
.padding()
NavigationLink("Go to Another View") {
AnotherView()
}
.padding()
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(10)
}
.navigationTitle("Detail")
}
}
private struct AnotherView: View {
var body: some View {
Text("Another Screen")
.font(.largeTitle)
.navigationTitle("Another")
}
}
/*
NavigationStack is the root container for navigation.
NavigationLink is used to navigate to a new view.
navigationTitle sets the title for the current view.
*/
2. Programmatic Navigation
You can programmatically control navigation using a path binding. This is useful for deep linking or conditional navigation.
import SwiftUI
struct NavigationStackExample2: View {
@State private var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
VStack {
Text("Home Screen")
.font(.largeTitle)
.padding()
Button("Go to Detail View") {
path.append("Detail")
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
Button("Go to Another View") {
path.append("Another")
}
.padding()
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(10)
}
.navigationTitle("Home")
.navigationDestination(for: String.self) { view in
switch view {
case "Detail":
DetailView()
case "Another":
AnotherView()
default:
EmptyView()
}
}
}
}
}
private struct DetailView: View {
var body: some View {
Text("Detail Screen")
.font(.largeTitle)
.navigationTitle("Detail")
}
}
private struct AnotherView: View {
var body: some View {
Text("Another Screen")
.font(.largeTitle)
.navigationTitle("Another")
}
}
3. Passing Data Between Views
You can pass data between views using NavigationLink or programmatic navigation.
import SwiftUI
struct NavigationStackExample3: View {
@State private var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
VStack {
Text("Home Screen")
.font(.largeTitle)
.padding()
Button("Go to Detail View with Data") {
path.append(User(name: "John Doe", age: 30))
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.navigationTitle("Home")
.navigationDestination(for: User.self) { user in
DetailView(user: user)
}
}
}
}
private struct User: Hashable {
let name: String
let age: Int
}
private struct DetailView: View {
let user: User
var body: some View {
VStack {
Text("Detail Screen")
.font(.largeTitle)
.padding()
Text("Name: \(user.name)")
Text("Age: \(user.age)")
}
.navigationTitle("Detail")
}
}
4. Handling Deep Links
You can handle deep links by programmatically setting the navigation path.
import SwiftUI
struct NavigationStackExample4: View {
@State private var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
VStack {
Text("Home Screen")
.font(.largeTitle)
.padding()
Button("Simulate Deep Link") {
path.append("Detail")
path.append("Another")
}
.padding()
.background(Color.purple)
.foregroundColor(.white)
.cornerRadius(10)
}
.navigationTitle("Home")
.navigationDestination(for: String.self) { view in
switch view {
case "Detail":
DetailView()
case "Another":
AnotherView()
default:
EmptyView()
}
}
}
}
}
private struct DetailView: View {
var body: some View {
Text("Detail Screen")
.font(.largeTitle)
.navigationTitle("Detail")
}
}
private struct AnotherView: View {
var body: some View {
Text("Another Screen")
.font(.largeTitle)
.navigationTitle("Another")
}
}
5. Using NavigationView (for iOS 15 and earlier)
If you're targeting iOS 15 or earlier, use NavigationView instead of NavigationStack.
import SwiftUI
struct NavigationStackExample5: View {
var body: some View {
NavigationView {
VStack {
Text("Home Screen")
.font(.largeTitle)
.padding()
NavigationLink("Go to Detail View") {
DetailView()
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.navigationTitle("Home")
}
}
}
private struct DetailView: View {
var body: some View {
Text("Detail Screen")
.font(.largeTitle)
.navigationTitle("Detail")
}
}
/*
Use NavigationStack for modern apps (iOS 16+).
Use NavigationPath for programmatic navigation and deep linking.
Keep your navigation hierarchy simple and avoid deeply nested stacks.
*/
This tutorial covers the basics of creating a navigation stack in SwiftUI. You can expand on this by adding more complex navigation logic, such as modals, sheets, and custom transitions. Let me know if you need further assistance!
