PureNavigationPath 1.1.0

PureNavigationPath 1.1.0

HoSiTuan 维护。



SwiftUI PureNavigationPath

Version License Platform

SwiftUI 持续改进,而随着 SwiftUI 支持 iOS 16.0 及更高版本,它提供了 NavigationPath,这使得我们更容易管理导航和 poppush 视图。尽管它解决了旧版本的一些限制,但仍有一些问题与弹出返回指定视图以及识别当前在 NavigationStack 中包含的项目有关。

我以一种完全基于 SwiftUI 的方式扩展了它,只使用现有的东西,并以最基本的方式编写。

现在我们可以根据我们附加到 NavigationPath 中的 Codable 对象弹出 NavigationStack 中的任何 View。查看下面的演示视频并使用它,如果它对您的项目有所帮助。

演示

示例

要运行示例项目,克隆仓库,然后首先从 Examples 目录运行 pod install

要求

  • iOS 16.0 或更高版本
  • Swift 5+

安装

PureNavigationPath 可通过 CocoaPods 获取。要安装它,只需在 Podfile 中添加以下行

pod 'PureNavigationPath'

用法

声明

使用 NavigationPath 创建 NavigationStack 声明目的地

NavigationStack(path: $path) {
    // ***
}
.navigationDestination(type: BookCategory.self, destination: { category in
    ViewB(category: category)
})
.navigationDestination(type: Book.self, destination: { book in
    ViewC(book: book)
})
.navigationDestination(type: String.self, destination: { path in
    ViewD(path: path)
})
.navigationDestination(type: Int.self, destination: { intPath in
    ViewE(intPath: intPath)
})

推到视图

根据上面定义的类型推送目的地,通过将 Codable 添加到路径。

struct Book: Identifiable, Hashable, Codable {}
let book = Book() // Codable struct
path.append(book)

enum BookCategory: Identifiable, Hashable, Codable {}
let category = BookCategory() 
path.append(category)

let pathString = "examplePath"
path.append(pathString)

let pathInteger = 100
path.append(pathInteger)

弹出视图

关闭导航的最顶层视图。

path.pop()

返回指定视图

基于原始可编码附加的任何视图返回。

path.pop(to: book)
path.pop(to: category)
path.pop(to: "examplePath")
path.pop(to: 100)

返回根视图

path.popToRoot()

获取当前导航堆栈

print(path.resolvedItems())
/*
[
    1,
    "ViewD", 
    Example.Book(id: "253D9737-51B7-44FF-A3F2-55C0ADF0AF35", name: "To Kill a Mockingbird", author: "Harper Lee", page: 281), 
    Example.BookCategory.new
]
 */

作者

工作联系。

许可协议

PureNavigationPath 采用 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。