NavigationViewKit

中文版说明

NavigationViewKit is a NavigationView extension library for SwiftUI.

For more detailed documentation and demo, please visit 用NavigationViewKit增强SwiftUI的导航视图

The extension follows several principles.

  • Non-disruptive

Any new feature does not affect the native functionality provided by Swiftui, especially if it does not affect the performance of Toolbar, NavigationLink in NavigationView

  • Be as easy to use as possible

    Add new features with minimal code

  • SwiftUI native style

    Extensions should be called in the same way as the native SwiftUI as much as possible

NavigationViewManager

Introduction

One of the biggest complaints from developers about NavigationView is that it does not support a convenient means of returning to the root view. There are two commonly used solutions.

  • Repackage the UINavigationController

    A good wrapper can indeed use the many functions provided by UINavigationController, but it is very easy to conflict with the native methods in SwiftUI, so you can’t have both.

  • Use procedural `NavigationLink

    Return the programmed NavigationLink (usually isActive) by undoing the root view. This means will limit the variety of NavigationLink options, plus is not conducive to implementation from non-view code.

NavigationViewManager is the navigation view manager provided in NavigationViewKit, it provides the following functions.

  • Can manage all the NavigationView in the application
  • Support to return the root view directly from any view under NavigationView by code
  • Jump to a new view via code from any view under NavigationView (no need to describe NavigationLink in the view)
  • Return to the root view via NotificatiionCenter for any NavigationView in the specified application
  • By NotificatiionCenter, let any NavigationView in the application jump to the new view
  • Support transition animation on and off

Register NavigationView

Since NavigationgViewManager supports multiple navigation views management, you need to register for each managed navigation view.

<div class="highlight highlight-source-swift position-relative" data-snippet-clipboard-copy-content="import NavigationViewKit
NavigationView {
List(0..

import NavigationViewKit
NavigationView {
            List(0..<10) { _ in
                NavigationLink("abc", destination: DetailView())
            }
        }
        .navigationViewManager(for: "nv1", afterBackDo: {print("back to root") })