ToastSwiftUI

在 SwiftUI 中显示 Toast 或弹出窗口的简单方法

关于

SwiftUI 是 Apple 在 2019 年带给 iOS 开发者的一个很棒的东西。但它仍然没有提供一种显示 Toast(短时消息)的方法。Toast 消息在 iOS 应用程序中非常流行,即使它不是一个原生视图。这个 ToastSwiftUI 开源库将帮助您轻松实现这一点。

显示弹出窗口也是一样的,不算太难,但没有原生的 API 可以使用。这个开源库也能帮助您。

Demo ToastSwiftUI

示例

要运行示例项目,请克隆 repo,然后首先从 Example 目录运行 pod install

要求

安装

Cocoapod

ToastSwiftUI 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

pod 'ToastSwiftUI'

然后在您的命令行中运行 pod install

Swift Package Manager

在 Xcode 中,选择菜单 File -> Swift Packages -> Add Package Dependency。选择一个目标,然后将此链接添加到输入字段:https://github.com/huynguyencong/ToastSwiftUI.git

手动

有时您不想使用 Cocoapod 安装。在这种情况下,您需要手动添加它。这非常简单,只需将 ToastSwiftUI/Classes 中的 Swift 文件添加到您的项目中即可。

用法

显示弹出窗口

@State private var isPresentingPopup = false
.popup(isPresenting: $isPresentingPopup, overlayColor: Color.black.opacity(0.4)) {
    MyPopup(isPresenting: $isPresentingPopup)
        .frame(width: 300, height: 500)         // it is not required
}
self.isPresentingPopup = true

显示 toast

@State private var isPresentingToast = false
.toast(isPresenting: $isPresentingToast, message: "Success", icon: .success)
self.isPresentingPopup = true

显示带有消息状态变量的 toast

@State private var toastMessage: String?
.toast($toastMessage)
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)
    }
}

自定义

popup 修饰符参数

toast 修饰符参数

UIKit 版本

UIKit 的 EzPopup

作者

Huy Nguyen, conghuy2012@gmail.com

许可证

ToastSwiftUI 在 MIT 许可证下可用。有关更多信息,请参见 LICENSE 文件。