EasyToast 是一个轻量级且可定制的 SwiftUI 包,它提供了易于使用的 Toast 通知。只需少量代码即可向您的用户显示简短的消息。
onTap
闭包,以便在点击 Toast 时添加自定义行为。您可以使用 Swift 包管理器 将 EasyToast 添加到您的项目中。
File > Add Packages Dependencies...
或者,将以下内容添加到您的 Package.swift
文件中
dependencies: [
.package(url: "https://github.com/banghuazhao/EasyToast.git", from: "0.4.0")
]
要显示带有消息的简单 Toast,请使用 easyToast
修饰符
import EasyToast
struct ContentView: View {
@State private var showToast = false
var body: some View {
content
.easyToast(isPresented: $showToast, message: "Hello, EasyToast!")
}
}
要显示带有消息的简单 Toast,请使用 easyToast
修饰符
var body: some View {
content
.easyToast(isPresented: $showToast, message: "This is a toast message on top", position: .top)
}
自定义外观和行为
let customStyle = ToastStyle(
backgroundColor: .blue,
textColor: .white,
font: .headline,
cornerRadius: 12,
shadow: .gray,
padding: EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16)
)
Text("Custom Toast")
.easyToast(
isPresented: $showToast,
message: "This is a custom toast message.",
position: .bottom,
duration: 3
style: customStyle
)
要显示自定义设计的 Toast 视图,请将 easyToast
修饰符与自定义视图一起使用
var body: some View {
content
.customToast(isPresented: $showToast, duration: 3, position: .bottom) {
HStack {
Image(systemName: "checkmark.circle")
.foregroundColor(.white)
Text("Show Custom Toast Success")
.foregroundColor(.white)
}
.padding()
.background(Color.green)
.cornerRadius(20)
}
}
@State private var showToast = false
var body: some View {
VStack {
Button("Show Toast") {
showToast = true
}
}
.easyToast(
isPresented: $showToast,
message: "Tap to dismiss",
onTap: {
print("Toast tapped, dismissing")
showToast = false
}
)
}
Toast 的实现方式是在应用 .easyToast
修饰符的视图之上覆盖一个自定义视图,确保它无缝地出现在当前内容之上,而不会中断底层布局
EasyToast 在 MIT 许可证下发布。有关详细信息,请参见 LICENSE。