к каталогу

📦 SwiftfulRouting/ SwiftfulThinking

Programmatic navigation for SwiftUI applications.

Открыть на GitHubобновлён 1мес назад
Звёзды
986
Форки
73
За неделю
За месяц
Рост %
Язык
Swift

Установка и запуск

How to use this package:

Quick Start (TLDR)

<details> <summary> Details (Click to expand) </summary> <br>

Use a RouterView to replace NavigationStack in your SwiftUI code.

Before SwiftfulRouting:

NavigationStack {
  MyView()
    .navigationDestination()
    .sheet()
    .fullScreenCover()
    .alert()
}

With SwiftfulRouting:

RouterView { _ in
  MyView()
}

Use a router to perform actions.

struct MyView: View {
    
    @Environment(\.router) var router
    
    var body: some View {
        Text("Hello, world!")
            .onTapGesture {
                router.showScreen { _ in 
                    AnotherView()
                }
            }
    }
}

All available methods in router are in AnyRouter.swift.

Examples:

router.showScreen()
router.showAlert()
router.showModal()
router.showTransition()
router.showModule()
router.dismissScreen()
router.dismissAlert()
router.dismissModal()
router.dismissTransition()
router.dismissModule()
</details>

Setup

<details> <summary> Details (Click to expand) </summary> <br> Add the package to your Xcode project.
https://github.com/SwiftfulThinking/SwiftfulRouting.git

Import the package.

import SwiftfulRouting

Add a RouterView at the top of your view heirarchy. A RouterView will embed your view into a NavigationStack and add modifiers to support all potential segues. This would replace an existing NavigationStack in your code.

Use a RouterView to replace NavigationStack in your SwiftUI code.

// Before SwiftfulRouting
NavigationStack {
  MyView()
    .navigationDestination()
    .sheet()
    .fullScreenCover()
    .alert()
}

// With SwiftfulRouting
RouterView { _ in
  MyView()
}

All child views have access to a Router in the Environment.

@Environment(\.router) var router
    
var body: some View {
     Text("Hello, world!")
          .onTapGesture {
               router.showScreen(.push) { _ in
                    Text("Another screen!")
               }
          }
     }
}

Instead of relying on the Environment, you can also pass the router directly into the child views.

RouterView { router in
    MyView(router: router)
}

You can also use the returned router directly. A new router is created and added to the view heirarchy after each segue and are therefore unique to each screen. In the below example, the tap gesture on "View3" could call dismissScreen() from router2 or router3, which would have different behaviors. This is done on purpose and is further explained in the docs below!

Из README репозитория · полный README на GitHub

Категории

Теги

iosswiftswiftui