SwiftUIToastManager
它能做什么?
SwiftUIToastManager 是一个用于管理屏幕上 toast 视图(从屏幕底部弹出的卡片)的工具。您可以为/删除、隐藏/显示 toast 等牌,并且在浏览应用时它们可以停留在屏幕上。
将其包含到你的项目
- Cocoapod: 将
pod 'SwiftUIToastManager'
添加到你的 Podfile 中
用法
- 初始化一个必须显示 toast 的视图
NavigationView {
... Your view code
}
.hasToasts([Toast1().eraseToAnyToast(), Toast2().eraseToAnyToast()])
- 隐藏一个视图中的 toast
struct MyView: View {
@EnvironmentObject
private var toastManager: BottomToastManager
var body: some View {
VStack {
... Your view code
}
.onAppear {
toastManager.hide(ToastType1())
}
}
}
- 显示一个 toast(将已初始化的 toast 带回来)与隐藏类似
toastManager.show(ToastType1())
- 从一个视图将 toast 插入到 toast 栈中
.onAppear {
toastManager.insert(Toast3().eraseToAnyToast())
}
- 将 toast 从栈中永久移除与插入类似
toastManager.remove(ToastType3())
查看 /Example 了解简单实现。