Navify is a SwiftUI package that simplifies navigation in your app by using a router and coordinator pattern. It provides a clean and organized way to handle navigation, presentation, and alerts.
- Navigation using Router and Coordinator pattern
- Push, Pop, Present, and Present Full Screen navigation styles
- Supports custom transitions for presentation
- Alert and Confirmation Dialog support
- Detents support for presentation
- SwiftUI-based
Navify requires iOS 16 and Xcode 14.
-
In Xcode go to
File
➤Add Packages...
-
In the top right corner, paste
https://github.com/username/Navify.git
and press Enter. -
Choose
Navify
from the list ➤Add Package
- To use Navify correctly, you must import it in every file that contains Navify content.
import Navify
- Next, you need a Router enum which needs to conform to the Router protocol. Afterwards you need to implement the id propertie and viewForDestination method as following:
import SwiftUI
import Navify
enum MyRouter: Router {
var id: String { UUID().uuidString }
// MARK: - Views
case YourView
// MARK: - Methods
func viewForDestination() -> some View {
switch self {
case .feed:
YourView()
}
}
- To ues the coordinator, you need to create a coordinator class where you have to conform to the Coordinator protocol. The Coordinator class must be a generic type of Router. If you conform to the Coordinator protocol, you need to implement following properties and methods:
import SwiftUI
import Navify
class MyCoordinator<R: Router>: ObservableObject, Coordinator {
// MARK: - Properties
@Published var screens: [Screen<R>] = []
@Published var alert: NavifyAlert = .init(title: "")
// MARK: - Methods
func popToRoot() {
screens = []
}
func navigateTo(_ view: R, style: Types) {
screens.append(Screen(style: style, view: view))
}
func presentAlert(with alert: NavifyAlert) {
self.alert = alert
}
}
If you don't want to use a alert, you can remove the alert propertie and method.
- Afterwards you need the NavifyStack which is similar like the NavigationStack.
NavifyStack(screens: Binding<[Screen<D>]>, alert: Binding<NavifyAlert>, content: () -> Content)
The alert parameter is optional. If you choose not to use alerts in your app, you can simply use the screens parameter instead.
- To navigate between views, you can use the methods provided by the coordinator.
/// Navigates to a view
navigateTo(view: Router, style: Types)
/// Presents an alert
presentAlert(with: NavifyAlert)
/// Pops to root
popToRoot()
If you leave the message parameter empty, the confirmation dialog will be displayed without a message.
- To dismiss a view, you can use the default environment property that is provided with SwiftUI.
@Environment(\.dismiss) private var dismiss