AppRouter
is a Swift package designed to streamline navigation in SwiftUI applications.
- Custom Route Definitions: Define app-specific routes using
AppRoute
protocol. - Versatile Presentation Modes: Support for page navigation, fullscreen covers, and sheet presentations.
Implement the AppRoute
protocol to create custom routes for your app.
public enum Route: AppRoute {
case home
case profile(_ userId: String)
case settings
@ViewBuilder
var content: some View {
switch self {
case .home:
HomeView()
case .profile(let userId):
ProfileView(userId: userId)
case .settings:
SettingsView()
}
}
}
Define a router type with your custom routes.
public typealias Router = AppRouter<AppRoute>
Use the Router.Stack
in your SwiftUI views to implement navigation.
- Suggested to add at the root view of your app.
- Add Stacks to create nested navigation flows.
Router.Stack {
HomeView() // Your root view
}
Navigate using Link
or StackController
.
struct HomeView: View {
var body: some View {
VStack {
Router.Link(to: .profile("user-id")) {
Text("Go to Profile")
}
Router.Link(to: .settings, presentation: .sheet([.medium])) {
Text("Open Settings")
}
}
}
}
struct HomeView: View {
@EnvironmentObject
var stackController: Router.StackController
var body: some View {
VStack {
Button("Go to Profile") {
stackController.push(route: .profile("user-id"))
}
Button("Open Settings") {
stackController.present(route: .settings, with: [.medium])
}
}
}
}