目前在 SwiftUI 中,通知用户某个进程完成的唯一方式是使用 Alert
。 有时,您只想显示一条消息,告诉用户某些事情已完成或他们的消息已发送。 尽管 Apple 使用各种类型的弹出窗口,但他们没有提供任何除 Alert 之外的方法。 这导致较差的用户体验,因为用户必须为他们应该被告知的每条信息点击“确定”或“取消”。
Alert Toast 是 Github 上的一个开源库,可与 SwiftUI 一起使用。 它允许您呈现不需要任何用户操作即可关闭或验证的弹出窗口。 一些很棒的用法示例:Message Sent
(消息已发送),Poor Network Connection
(网络连接不良),Profile Updated
(个人资料已更新),Logged In/Out
(已登录/已注销),Favorited
(已收藏),Loading
(加载中) 等...
Alert
(在中心弹出),HUD
(从顶部掉落)和 Banner
(从底部弹出/滑动)。Complete
(完成), Error
(错误), SystemImage
(系统图片), Image
(图片), Loading
(加载中), 和 Regular
(普通,仅文本)。₿ 比特币捐赠地址
0xec48bfa813a773fa2394aec23f97da5cb4d5ff02
CocoaPods 是 Cocoa 项目的依赖项管理器。 有关用法和安装说明,请访问他们的网站。 要使用 CocoaPods 将 AlertToast
集成到您的 Xcode 项目中,请在您的 Podfile 中指定它
pod 'AlertToast'
Swift Package Manager 是一种用于管理 Swift 代码分发的工具。 它与 Swift 构建系统集成,可自动执行下载,编译和链接依赖项的过程。
要使用 Xcode 12 将 AlertToast
集成到您的 Xcode 项目中,请在 File > Swift Packages > Add Package Dependency...
中指定它
https://github.com/elai950/AlertToast.git, :branch="master"
对于 Xcode 13,请参考这篇文章来安装 AlertToast
如果您不想使用任何依赖项管理器,则可以手动将 AlertToast
集成到您的项目中。 将 Sources/AlertToast
文件夹放在您的 Xcode 项目中。 确保启用 Copy items if needed
和 Create groups
。
首先,在要使用 AlertToast
的每个 swift
文件上添加 import AlertToast
。
然后,使用 .toast
视图修饰符
参数
isPresenting
:(必须)分配一个 Binding<Bool>
以显示或关闭警报。duration
:默认值为 2,设置为 0 以禁用自动关闭。tapToDismiss
:默认为 true
,设置为 false
以禁用。alert
:(必须)期望 AlertToast
。import AlertToast
import SwiftUI
struct ContentView: View{
@State private var showToast = false
var body: some View{
VStack{
Button("Show Toast"){
showToast.toggle()
}
}
.toast(isPresenting: $showToast){
// `.alert` is the default displayMode
AlertToast(type: .regular, title: "Message Sent!")
//Choose .hud to toast alert from the top of the screen
//AlertToast(displayMode: .hud, type: .regular, title: "Message Sent!")
//Choose .banner to slide/pop alert from the bottom of the screen
//AlertToast(displayMode: .banner(.slide), type: .regular, title: "Message Sent!")
}
}
}
.toast(isPresenting: $showAlert, duration: 2, tapToDismiss: true, alert: {
//AlertToast goes here
}, onTap: {
//onTap would call either if `tapToDismis` is true/false
//If tapToDismiss is true, onTap would call and then dismis the alert
}, completion: {
//Completion block after dismiss
})
AlertToast(displayMode: DisplayMode,
type: AlertType,
title: Optional(String),
subTitle: Optional(String),
style: Optional(AlertStyle))
//This is the available customizations parameters:
AlertStyle(backgroundColor: Color?,
titleColor: Color?,
subTitleColor: Color?,
titleFont: Font?,
subTitleFont: Font?)
SFSymbols
的名称图片。.toast(isPresenting: Binding<Bool>, duration: Double = 2, tapToDismiss: true, alert: () -> AlertToast , onTap: () -> (), completion: () -> () )
AlertToast(type: .regular, title: Optional(String), subTitle: Optional(String))
AlertToast(type: .complete(Color)/.error(Color), title: Optional(String), subTitle: Optional(String))
AlertToast(type: .systemImage(String, Color), title: Optional(String), subTitle: Optional(String))
AlertToast(type: .image(String), title: Optional(String), subTitle: Optional(String))
//When using loading, duration won't auto dismiss and tapToDismiss is set to false
AlertToast(type: .loading, title: Optional(String), subTitle: Optional(String))
您可以在单个视图上添加多个 .toast
。
我写了一篇文章,其中包含更多用法示例。
Medium - 如何在 SwiftUI 中 Toast 警报
欢迎并感谢所有问题报告,功能请求,Pull Request 和 GitHub Star。
Elai Zuberman
AlertToast
在 MIT 许可证下可用。 有关更多信息,请参见LICENSE文件。