SwiftUI 中显示 toast 的简单方法
概述
ToastUI
为您提供了一个简单的方式,在 SwiftUI 中显示 toast、抬头显示 (HUD)、自定义警报或任何 SwiftUI 视图。
入门指南
以下是一个示例,如何显示一个不计时的进度指示器 HUD,并在 2 秒后消失。
struct ContentView: View {
@State private var presentingToast: Bool = false
var body: some View {
Button {
presentingToast = true
} label: {
Text("Tap me")
.bold()
.foregroundColor(.white)
.padding()
.background(Color.accentColor)
.cornerRadius(8.0)
}
.toast(isPresented: $presentingToast, dismissAfter: 2.0) {
print("Toast dismissed")
} content: {
ToastView("Loading...")
.toastViewStyle(IndefiniteProgressToastViewStyle())
}
}
}
您还可以显示自定义警报或您选择的任何 SwiftUI 视图。
struct ContentView: View {
@State private var presentingToast: Bool = false
var body: some View {
Button {
presentingToast = true
} label: {
Text("Tap me")
.bold()
.foregroundColor(.white)
.padding()
.background(Color.accentColor)
.cornerRadius(8.0)
}
.toast(isPresented: $presentingToast) {
ToastView {
VStack {
Text("Hello from ToastUI")
.padding(.bottom)
.multilineTextAlignment(.center)
Button {
presentingToast = false
} label: {
Text("OK")
.bold()
.foregroundColor(.white)
.padding(.horizontal)
.padding(.vertical, 12.0)
.background(Color.accentColor)
.cornerRadius(8.0)
}
}
}
}
}
}
请查看 ToastUISample
项目以获取更多信息,并查看下方的 文档。
要求
- iOS 14.0+ | tvOS 14.0+ | macOS 11.0+
- Xcode 12.0+ | Swift 5.3+
安装
Swift 包管理器
ToastUI
可以通过 Swift 包管理器 获取。
为了集成到应用中,将 ToastUI
添加到现有的 Xcode 项目中作为包依赖
- 从 文件 菜单中选择 Swift Packages > 添加包依赖...
- 将 https://github.com/quanshousio/ToastUI 输入到包仓库 URL 文本框中。
- Xcode 应当默认选择更新到下一个版本选项。
对于包集成,将以下行添加到 Package.swift
中的 dependencies
参数。
dependencies: [
.package(url: "https://github.com/quanshousio/ToastUI.git", from: "2.0.0")
]
CocoaPods
ToastUI
可以通过 CocoaPods 获取。要安装,将以下行添加到您的 Podfile
。
pod 'ToastUI'
文档
更多详细文档,请查阅 此处。
呈现
ToastUI
支持在任何位置呈现任何 SwiftUI 视图。您只需添加 toast()
视图修饰符并提供视图,就像使用 alert()
或 sheet()
一样。
.toast(isPresented: $presentingToast) {
// your SwiftUI views here
}
toast()
视图修饰符有两种类型。更多使用信息,请查看 示例。
toast(isPresented:dismissAfter:onDismiss:content:)
– 当给定的布尔绑定为真时显示吐司。toast(item:dismissAfter:onDismiss:content:)
– 使用提供的项目作为吐司内容的来源来显示吐司。
ToastView
ToastUI
包含 ToastView
,它以一个包含您提供的视图的圆角矩形形式可视化表示,并具有默认的浅模糊背景。
.toast(isPresented: $presentingToast) {
ToastView("Hello from ToastUI")
}
下方的图示演示了 ToastView
的布局。
+-----------------------------+
| |
| <Background> |
| |
| +-----------+ |
| | | |
| | <Content> | |
| | | |
| | | |
| | <Label> | |
| +-----------+ |
| |
| |
| |
+-----------------------------+
ToastView(<Label>) {
<Content>
} background: {
<Background>
}
ToastView
与自定义内容视图和自定义背景视图。
.toast(isPresented: $presentingToast) {
ToastView("Saved!") {
// custom content views
Image(systemName: "arrow.down.doc.fill")
.font(.system(size: 48))
.foregroundColor(.green)
.padding()
} background: {
// custom background views
Color.green.opacity(0.1)
}
}
ToastView
使用内置样式且无背景。
.toast(isPresented: $presentingToast) {
ToastView("Loading...") {
// EmptyView()
} background: {
// EmptyView()
}
.toastViewStyle(IndefiniteProgressToastViewStyle())
}
样式
ToastUI
默认支持七种不同的 ToastViewStyle
。您必须使用 ToastView
并以 toastViewStyle(_:)
修饰符设置相应的样式。所有样式都支持原生动态类型以支持无障碍访问。
DefaultProgressToastViewStyle()
– 用户没有提供内容时显示一个空吐司。ToastView
默认使用此样式。IndefiniteProgressToastViewStyle()
– 显示一个无限循环的圆形进度指示器。DefiniteProgressToastViewStyle(value:total:)
– 显示从0到100%的确定圆形进度指示器。SuccessToastViewStyle()
– 显示一个成功吐司。ErrorToastViewStyle()
– 显示一个错误吐司。WarningToastViewStyle()
- 显示一个警告吐司。InfoToastViewStyle()
– 显示一个信息吐司。
ToastUI
通过 cocoaBlur()
视图修饰符包含了一个 UI/NSVisualEffectView
包装器,这比在 SwiftUI 中现有的 blur(radius:opaque:)
更灵活。
cocoaBlur(blurStyle:vibrancyStyle:blurIntensity:)
- 用于 iOS。cocoaBlur(blurStyle:blurIntensity:)
- 用于 tvOS。cocoaBlur(material:blendingMode:state:)
- 用于 macOS。
贡献
所有的问题报告、功能请求、pull请求和GitHub星星都很受欢迎,并且备受赞赏。
作者
Quan Tran (@quanshousio)
致谢
- Fruta - 苹果提供的用于 SwiftUI 的
UI/NSVisualEffectView
包装器。 - Label - 五颗星关于 SwiftUI
Label
和样式擦除器的资讯文章。 - SVProgressHUD - 由 Sam Vermette 和 Tobias Tiemerding 设计的圆形进度 HUD 的原始设计。
- SwiftUI 自定义样式 - Swift UI Lab 关于 SwiftUI 自定义样式的资讯文章。
许可证
ToastUI
适用于 MIT 许可。有关更多信息,请参阅 LICENSE 文件。