MSwiftUINavigator is a Swift package that provides a navigation solution for SwiftUI applications, leveraging the UIKit navigation system. It simplifies common navigation tasks and integrates seamlessly with SwiftUI views.
- Push views onto the navigation stack.
- Present views modally with custom transition and presentation styles.
- Dismiss views and navigate back.
- Pop to the root view.
- Present sheets with customizable sizes using the FittedSheets library.
- Present dialogs and action sheets above the current view.
- Pop to a specific view type in the navigation stack.
- Present dialogs and action sheets above the current view, with options for customizations.
You can add MSwiftUINavigator to your Swift package by adding it as a dependency in your Package.swift
file:
dependencies: [
.package(url: "https://github.com/MahmoudAbdelshafi/MSwiftUINavigator.git", .branch("main"))
],
To use MSwiftUINavigator in your SwiftUI project, you'll need to import it and conform to the Navigator protocol in the main view where you want to use the navigator:
import MSwiftUINavigator
struct ContentView: View, Navigator {
// Your view code here
}
Additionally, you can access the Navigator as an @Environment object:
@Environment(\.navigator) var navigator
navigator.presentSheet {
// Your view code here
}
navigator.pushView {
// Your view code here
}
To present an action sheet, you can use the presentActionSheet
function provided by MSwiftUINavigator:
navigator.presentActionSheet {
ActionSheet(
title: Text("Choose an action"),
message: Text("What would you like to do?"),
buttons: [
.default(Text("Option 1")) {
// Handle option 1
},
.default(Text("Option 2")) {
// Handle option 2
},
.cancel()
]
)
}
For singleton access to the NavigationManager, you can use the shared instance like this:
NavigationManager.shared.presentView(transitionStyle: .coverVertical,
presentStyle: .fullScreen,
animated: true) {
// Your View here
}
There's also an example project available on GitHub for reference: MSwiftUINavigatorExample.
MSwiftUINavigator relies on the following external dependency to enhance its functionality, particularly for handling popups, sheets, and dialogs:
- FittedSheets: FittedSheets is a powerful library available on GitHub that provides advanced capabilities for presenting sheets with customizable sizes and behaviors. MSwiftUINavigator utilizes FittedSheets to create dynamic and interactive sheet presentations, enhancing the user experience when displaying popups, sheets, and dialogs in your SwiftUI applications.
This package is released under the MIT License.