在 SwiftUI 中显示 Toast 或弹出窗口的简单方法
SwiftUI 是 Apple 在 2019 年带给 iOS 开发者的一个很棒的东西。但它仍然没有提供一种显示 Toast(短时消息)的方法。Toast 消息在 iOS 应用程序中非常流行,即使它不是一个原生视图。这个 ToastSwiftUI 开源库将帮助您轻松实现这一点。
显示弹出窗口也是一样的,不算太难,但没有原生的 API 可以使用。这个开源库也能帮助您。
要运行示例项目,请克隆 repo,然后首先从 Example 目录运行 pod install
。
ToastSwiftUI 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
pod 'ToastSwiftUI'
然后在您的命令行中运行 pod install
。
在 Xcode 中,选择菜单 File -> Swift Packages -> Add Package Dependency。选择一个目标,然后将此链接添加到输入字段:https://github.com/huynguyencong/ToastSwiftUI.git
有时您不想使用 Cocoapod 安装。在这种情况下,您需要手动添加它。这非常简单,只需将 ToastSwiftUI/Classes
中的 Swift 文件添加到您的项目中即可。
@State private var isPresentingPopup = false
popup
修饰符添加到您的视图,并使用步骤 1 中的绑定变量。MyPopup
是您想作为弹出窗口显示的视图。如果您对弹出视图的固有大小感到满意,则设置 frame 不是必需的.popup(isPresenting: $isPresentingPopup, overlayColor: Color.black.opacity(0.4)) {
MyPopup(isPresenting: $isPresentingPopup)
.frame(width: 300, height: 500) // it is not required
}
self.isPresentingPopup = true
@State private var isPresentingToast = false
toast
修饰符添加到您的视图,并使用步骤 1 中的绑定变量。.toast(isPresenting: $isPresentingToast, message: "Success", icon: .success)
self.isPresentingPopup = true
String
@State
变量。当此可选变量具有值时,它将触发一个 toast。@State private var toastMessage: String?
toast
修饰符,将上述消息变量的 Binding
($) 作为第一个参数传递。.toast($toastMessage)
String
来显示 toast。您也可以通过将其设置为 nil
来关闭它toastMessage = "Hello world!"
请参阅下面的完整代码,它有 3 个示例,以不同的方式显示 toast 和弹出窗口
import SwiftUI
import ToastSwiftUI
struct ContentView: View {
// 1.1. Example 1: Add @State variables to control when showing the popup
@State private var isPresentingPopup = false
// 1.2. Example 2: First way to show a toast. Add @State variables to control when showing the toast
@State private var isPresentingToast = false
// 1.3. Example 3: Second way to show a toast. Add an optional String @State variables to control when showing the toast
@State private var toastMessage: String?
@State private var count = 0
var body: some View {
VStack(spacing: 20) {
Button("Show a success toast with a boolean variable") {
// 3.2.1. Set state variable to true if you want to show the toast
self.isPresentingToast = true
}
Button("Dismiss the success toast") {
// 3.2.2. Set state variable to false if you want to hide the toast
self.isPresentingToast = false
}
Divider()
Button("Show toast with a text binding") {
// 3.3.1. Set text variable you want to show
toastMessage = "Toast number \(count)"
count += 1
}
Button("Dismiss toast") {
// 3.3.2. Set text variable to nil
self.toastMessage = nil
}
Divider()
Button("Show popup") {
// 3.1.1. Set state variable to true if you want to show the popup
self.isPresentingPopup = true
}
Button("Dismiss popup") {
// 3.1.2. Set state variable to true if you want to hide the popup
self.isPresentingPopup = false
}
Spacer()
}
.padding()
// 2.1. Add a `popup` modifier to your view with the binding variable in step 1
.popup(isPresenting: $isPresentingPopup, popup:
MyPopup(isPresenting: $isPresentingPopup)
.background(Color(.systemBackground))
)
// 2.2. Add a `toast` modifier to your view with the binding variable in step 1
.toast(isPresenting: $isPresentingToast, message: "Success", icon: .success)
// 2.3. Add a `toast` modifier to your view with the binding variable in step 1
.toast($toastMessage)
}
}
autoDismiss
hasShadow
cornerRadius
overlayColor
isTapOutsideToDismiss
autoDismiss
icon
backgroundColor
textColor
Huy Nguyen, conghuy2012@gmail.com
ToastSwiftUI 在 MIT 许可证下可用。有关更多信息,请参见 LICENSE 文件。