Toast 提示

一个用于 SwiftUI 的 Toast 提示通知库。

Simulator Screen Recording - iPhone 15 Pro - 2024-09-16 at 11 53 37

Simulator Screen Recording - iPhone 16 Pro - 2024-09-18 at 10 53 57

SCR-20240916-kqog

功能特性

用法

  1. 在你的根视图中安装 Toast
import SwiftUI
import Toasts

@main
struct MyApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
        .installToast(position: .bottom)
    }
  }
}
  1. 展示一个 Toast 提示
@Environment(\.presentToast) var presentToast

Button("Show Toast") {
  let toast = ToastValue(
    icon: Image(systemName: "bell"),
    message: "You have a new notification."
  )
  presentToast(toast)
}

高级用法

presentToast(
  message: "Loading...",
  task: {
    // Handle loading task
    return "Success"
  },
  onSuccess: { result in
    ToastValue(icon: Image(systemName: "checkmark.circle"), message: result)
  },
  onFailure: { error in
    ToastValue(icon: Image(systemName: "xmark.circle"), message: error.localizedDescription)
  }
)

自定义

image
let toast = ToastValue(
  message: "Message only toast."
)
let toast = ToastValue(
  message: "Toast with action required.",
  button: ToastButton(title: "Confirm", color: .green, action: {
    // Handle button action
  })
)

系统要求