AlertToast-SwiftUI Tweet

在 SwiftUI 中呈现类似 Apple 的 Alert 和 Toast

🌄 示例

🔭 概述

目前在 SwiftUI 中,通知用户某个进程完成的唯一方式是使用 Alert。 有时,您只想显示一条消息,告诉用户某些事情已完成或他们的消息已发送。 尽管 Apple 使用各种类型的弹出窗口,但他们没有提供任何除 Alert 之外的方法。 这导致较差的用户体验,因为用户必须为他们应该被告知的每条信息点击“确定”或“取消”。

Alert Toast 是 Github 上的一个开源库,可与 SwiftUI 一起使用。 它允许您呈现不需要任何用户操作即可关闭或验证的弹出窗口。 一些很棒的用法示例:Message Sent (消息已发送),Poor Network Connection (网络连接不良),Profile Updated (个人资料已更新),Logged In/Out (已登录/已注销),Favorited (已收藏),Loading (加载中) 等...

         

如果您喜欢这个项目,请不要忘记给个星星 🌟

      

₿ 比特币捐赠地址

0xec48bfa813a773fa2394aec23f97da5cb4d5ff02

导航

💻 安装

Cocoapods

AlertToast Cocapods 网站

CocoaPods 是 Cocoa 项目的依赖项管理器。 有关用法和安装说明,请访问他们的网站。 要使用 CocoaPods 将 AlertToast 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

pod 'AlertToast'

Swift 包管理器

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 neededCreate groups

🧳 要求

🛠 用法

首先,在要使用 AlertToast 的每个 swift 文件上添加 import AlertToast

然后,使用 .toast 视图修饰符

参数

使用常规警报的用法示例

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?)

可用警报类型

警报对话框视图修饰符(使用默认设置)

.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文件。