在 SwiftUI 中呈现像 Apple 一样的报警和提示信息
🌄 示例
🔭 概述
目前在 SwiftUI 中,唯一的通知用户某个过程结束的方法是通过使用 Alert
。有时,您只想弹出一个消息,告诉用户某个任务已完成或消息已发送。尽管 Apple 使用了各种不同的弹出窗口,但 Apple 没有提供其他方法来使用 Alert
。结果用户体验差,用户需要为每个需要通知的信息点击 "OK/Dismiss"。
Alert Toast 是一个开源库,用于与 SwiftUI 一起使用,它可以显示不需要用户操作来关闭或验证的弹出窗口。一些优秀的用法示例包括:消息已发送
,网络连接差
,个人资料更新
,登录/登出
,收藏
,加载中
等等...
基于纯 SwiftUI 开发。
- 支持:
- 3种显示模式:
Alert
(弹在中心),HUD
(从顶部下落)和Banner
(从底部弹出/滑动)。 完成
,错误
系统图像
,图像
,加载
和常规
(只有文本)。- 支持浅色和深色模式。
- 与任何类型的视图构建器兼容。
- 支持本地化。
- 字体和背景自定义。
Star ⭐
。
如果你喜欢这个项目,别忘了给自己点个比特币捐赠地址
0xec48bfa813a773fa2394aec23f97da5cb4d5ff02
- 只向这个存款地址发送 BTC。
- 确保网络是 BNB 智能链 (BEP20)。
导航
💻 安装
Cocoapods
CocoaPods 是用于 Cocoa 项目的依赖项管理器。有关使用和安装说明,请访问他们的网站。要使用 CocoaPods 将 AlertToast
集成到您的 Xcode 项目中,请在其 Podfile 中指定它
pod 'AlertToast'
Swift 包管理器
Swift 包管理器 是一个用于管理和分发 Swift 代码的工具。它与 Swift 构建系统集成,以自动化下载、编译和链接依赖项的过程。
要使用 Swift 包管理器将 AlertToast
集成到您的 Xcode 项目中使用 Xcode 12,请按照以下步骤操作:文件 > Swift 包 > 添加包依赖...
https://github.com/elai950/AlertToast.git, :branch="master"
对于 Xcode 13,请参阅这篇文章以安装 AlertToast
手动
如果您不想使用任何依赖项管理器,您可以将 AlertToast
手动集成到项目中。将 Sources/AlertToast
文件夹放在您的 Xcode 项目中。请确保启用 如果需要,复制项
和 创建组
。
🧳 要求
- iOS 13.0+ | macOS 11+
- Xcode 12.0+ | Swift 5+
🛠 使用
首先,在每个您想使用 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
})
Alert Toast 参数
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
的名称图片。 - 图片:来自 Assets 的名称图片。
- 加载:活动指示器(旋转器)。
具有默认设置的提示框视图修饰器
.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
。
📖 文章
我写了一篇包含更多用法示例的文章。
👨💻 贡献者
所有问题报告、功能请求、拉取请求和GitHub星标都受到欢迎并受到高度重视。
✍️ 作者
Elai Zuberman
📃 许可
AlertToast
在MIT许可下可用。更多信息请参阅 LICENSE 文件。