NavigationView

repository·main·Indexed 43 days ago

https://github.com/mijick/navigationview

A SwiftUI library for simplifying complex navigation patterns. It provides custom animations, gesture support, automatic scroll view offset preservation, and a protocol-based system using NavigatableView and NavigationManager for pushing and popping views.

Tokens
1K
Snippets
5
Records
7
Agent score
6%

What's inside NavigationView

  1. Create a NavigatableView

    main

    To make a view eligible to be pushed onto the navigation stack, conform it to the NavigatableView protocol. You must implement the body property and can optionally implement configure(view: NavigationConfig) -> NavigationConfig to customize the view's appearance (e.g., background color).

    struct ExampleView: NavigatableView {
        // Optional: Customize view appearance
        func configure(view: NavigationConfig) -> NavigationConfig {
            view.backgroundColour(.red)
        }
    
        var body: some View {
            VStack(spacing: 0) {
                Text("Witaj okrutny świecie")
                Spacer()
                Button(action: pop) { Text("Pop") }
            }
        }
    }
    struct ExampleView: NavigatableView {
        func configure(view: NavigationConfig) -> NavigationConfig {
            view.backgroundColour(.red)
        }
        var body: some View {
            VStack(spacing: 0) {
                Text("Witaj okrutny świecie")
                Spacer()
                Button(action: pop) { Text("Pop") }
            }
        }
    }
  2. Setup the NavigationView library

    main

    To initialize the navigation system, call .implementNavigationView(config:) on your root view inside your @main App structure. The root view must be a NavigatableView.

    @main struct NavigationView_Main: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
                   .implementNavigationView(config: nil)                    
            }
        }
    }
  3. Install NavigationView via CocoaPods

    main

    To install using CocoaPods, follow these steps:

    1. Ensure CocoaPods 1.10.0 or later is installed.
    2. Initialize CocoaPods if you haven't already:
      pod init
    3. Add the dependency to your Podfile:
      pod 'MijickNavigationView'
    4. Install the dependency and generate the .xcworkspace file:
      pod install
    5. Open and use the new .xcworkspace file for your project.
    pod init
    pod 'MijickNavigationView'
    pod install
  4. Install NavigationView via Swift Package Manager

    main

    Add NavigationView as a dependency in your Package.swift file using the following URL and branch:

    dependencies: [
        .package(url: "https://github.com/Mijick/NavigationView", branch("main"))
    ]
  5. Advanced Navigation: New Roots, EnvironmentObjects, and Focus

    main

    Set a new navigation root

    Use .setAsNewRoot() after a push to replace the current navigation stack with the new view:

    ExampleView()
        .push(with: .verticalSlide)
        .setAsNewRoot()

    Pass EnvironmentObjects

    You can pass an EnvironmentObject to a view, but it must be done before calling .push():

    ExampleView()
        .environmentObject(object)
        .push(with: .verticalSlide)

    Detect view visibility with onFocus

    Instead of onAppear, use .onFocus(self) to be notified every time a view becomes the top-most visible view in the stack:

    struct ExampleView: NavigatableView {
        var body: some View {
            VStack {
                Text("Content")
            }
            .onFocus(self) {
                // Triggered when view is on top of the stack
            }
        }
    }
  6. Pop views from the navigation stack

    main

    There are two ways to navigate backward:

    1. Inside a NavigatableView

    Use the built-in methods provided by the protocol:

    • pop(): Goes back one screen.
    • pop(to type:): Returns to a specific view type.
    • popToRoot(): Returns to the very first view in the stack.
    Button(action: popToRoot) { Text("Tap to return to root") }

    2. Using NavigationManager

    Call static methods from the NavigationManager from anywhere in your code:

    • NavigationManager.pop()
    • NavigationManager.pop(to type:)
    • NavigationManager.popToRoot()
  7. Push a view to the navigation stack

    main

    You can push any NavigatableView from anywhere in your code by calling .push(with:) on an instance of that view. You can specify an animation type, such as .verticalSlide.

    struct SettingsViewModel {
        func openSettings() {
            ExampleView().push(with: .verticalSlide)
        }
    }
    ExampleView().push(with: .verticalSlide)