Skip to content

bannzai/NavigationController

Repository files navigation

NavigationController

NavigationController is a Swift Package designed to simplify and efficiently manage navigation within SwiftUI applications. Leveraging SwiftUI's NavigationStack and NavigationPath, it aims to make the implementation of navigation flows between screens as straightforward as possible. This package is created with the goal of

Features

  • Simple API for implementing navigation transitions
  • Utilizes SwiftUI's NavigationStack and NavigationPath for navigation management
  • Supports sharing navigation state across multiple screens
  • Facilitates programmatic navigation between views

Installation

Swift Package Manager

NavigationController can be added to your project using the Swift Package Manager. In Xcode, select "File" > "Swift Packages" > "Add Package Dependency..." and enter the following repository URL:

Usage

To use NavigationController, apply the WithNavigationModifier modifier to the SwiftUI view you want to manage navigation for. Then, control the navigation between screens using instances of NavigationController.

struct ContentView: View {
  var body: some View {
    RootView()
      .withNavigation()
  }
}

struct RootView: View {
  // Retrieve navigationController via EnvironmentObject
  @EnvironmentObject var navigationController: NavigationController

  var body: some View {
    VStack {
      // Use navigationController ...
    }
  }
}

To push a screen:

Button {
  navigationController.push { 
    YourDestinationView() 
  }
} label: {
  Text("Push")
}

To pop a screen

Button {
  navigationController.pop()
} label: {
  Text("Pop")
}

Edit path directly. For example edit path and pop.

Button {
  navigationController.path.removeLast()
} label: {
  Text("Edit path and pop")
}

LICENSE

NavigationController is released under the MIT License. For more details, please see the LICENSE file.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages