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
BaseNavigationStackmodule into your Swift file:
import BaseNavigationStack- Build In View:
structContentView:View{@StatevarnavigationRouter=BaseNavigationStack<ViewNavigationTarget,SheetViewPresentTarget>(
isPresented:.constant(.splash))varbody:someView{NavigationStack(path: navigationRouter.navigationPath){BaseView().withNavigationRouter().withSheetRouter(sheetDestination: navigationRouter.presentingSheet)}.environment(navigationRouter)}}- Quick To Push/Present View
structListView:View{@Environment(BaseNavigationStack<ViewNavigationTarget, SheetViewPresentTarget>.self)varnavigationRoutervarbody:someView{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()}}}