EasyToast

Version License

简介

EasyToast 是一个轻量级且可定制的 SwiftUI 包,它提供了易于使用的 Toast 通知。只需少量代码即可向您的用户显示简短的消息。

✨ 功能

🧳 要求

💻 安装

Swift 包管理器

您可以使用 Swift 包管理器 将 EasyToast 添加到您的项目中。

  1. 在 Xcode 中打开您的项目。
  2. 转到 File > Add Packages Dependencies...
  3. 输入包 URL:https://github.com/banghuazhao/EasyToast
  4. 选择最新版本

或者,将以下内容添加到您的 Package.swift 文件中

dependencies: [
    .package(url: "https://github.com/banghuazhao/EasyToast.git", from: "0.4.0")
]

🛠 使用方法

快速开始

要显示带有消息的简单 Toast,请使用 easyToast 修饰符

import EasyToast

struct ContentView: View {
    @State private var showToast = false

    var body: some View {
        content
            .easyToast(isPresented: $showToast, message: "Hello, EasyToast!")
    }
}

在顶部显示一个简单的 Toast

要显示带有消息的简单 Toast,请使用 easyToast 修饰符

var body: some View {
    content
        .easyToast(isPresented: $showToast, message: "This is a toast message on top", position: .top)
}

自定义

自定义外观和行为

let customStyle = ToastStyle(
    backgroundColor: .blue,
    textColor: .white,
    font: .headline,
    cornerRadius: 12,
    shadow: .gray,
    padding: EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16)
)

Text("Custom Toast")
    .easyToast(
        isPresented: $showToast,
        message: "This is a custom toast message.",
        position: .bottom,
        duration: 3
        style: customStyle
    )

显示自定义 Toast

要显示自定义设计的 Toast 视图,请将 easyToast 修饰符与自定义视图一起使用

var body: some View {
    content
        .customToast(isPresented: $showToast, duration: 3, position: .bottom) {
            HStack {
                Image(systemName: "checkmark.circle")
                    .foregroundColor(.white)
                Text("Show Custom Toast Success")
                    .foregroundColor(.white)
            }
            .padding()
            .background(Color.green)
            .cornerRadius(20)
        }
}

关闭 Toast

@State private var showToast = false

var body: some View {
    VStack {
        Button("Show Toast") {
            showToast = true
        }
    }
    .easyToast(
        isPresented: $showToast,
        message: "Tap to dismiss",
        onTap: {
            print("Toast tapped, dismissing")
            showToast = false
        }
    )
}

💡 想法

Toast 的实现方式是在应用 .easyToast 修饰符的视图之上覆盖一个自定义视图,确保它无缝地出现在当前内容之上,而不会中断底层布局

许可证

EasyToast 在 MIT 许可证下发布。有关详细信息,请参见 LICENSE。