Build a navigation controller with Navigation Stack SwiftUI
BaseNavigationStack is a Swift package that provides a flexible and reusable base class for managing navigation stacks in SwiftUI applications.
BaseNavigationStack
is designed to simplify the management of navigation stacks in SwiftUI applications. It introduces a generic approach to handle different types for views and presentation targets. This allows you to build navigation flows with ease, supporting various types of views and presentation styles.
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2023-12-14.at.17.52.24.mp4
You can use the Swift Package Manager to install BaseNavigationStack
by adding it to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/iletai/BaseNavigationStack.git", from: "1.0.0"),
],
targets: [
.target(name: "YourTarget", dependencies: ["BaseNavigationStack"]),
]
- Import the
BaseNavigationStack
module into your Swift file:
import BaseNavigationStack
- Build In View:
struct ContentView: View {
@State
var navigationRouter = BaseNavigationStack<
ViewNavigationTarget,
SheetViewPresentTarget
>(
isPresented: .constant(.splash)
)
var body: some View {
NavigationStack(path: navigationRouter.navigationPath) {
BaseView()
.withNavigationRouter()
.withSheetRouter(sheetDestination: navigationRouter.presentingSheet)
}
.environment(navigationRouter)
}
}
- Quick To Push/Present View
struct ListView: View {
@Environment(BaseNavigationStack<ViewNavigationTarget, SheetViewPresentTarget>.self)
var navigationRouter
var body: some View {
VStack {
Text("List View")
.font(.title)
.fontWeight(.bold)
HStack {
Button {
navigationRouter.pushToView(.splash)
} label: {
Text("Splash")
}
Button {
navigationRouter.popBack()
} label: {
Text("Pop Back")
}
Button {
navigationRouter.navigateToRoot()
} label: {
Text("Pop To Root")
}
}
.buttonStyle(.bordered)
Spacer()
}
}
}