English | 简体中文
A lightweight SwiftUI toast notification component that adheres to Apple's native design guidelines.
- Simple and easy-to-use API
- Support for different Toast types (success, error, warning, info)
- Customizable icons and display time
- Supports iOS 17+ and macOS 14+
- Developed with pure SwiftUI, supporting cross-platform
- Simple to use, no need for many customization options
- In Xcode, select "File" > "Add Packages..."
- Enter the URL of this repository
- Select version rules and click "Add Package"
Add the WSToast modifier to your App entry and use @_exported
to import the package:
// App main entry file
@_exported import WSToast // Use @_exported to avoid reimporting in subcomponents
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.wsToast() // Add Toast support
}
}
}
Use the global function showToast()
, which can be called anywhere in your app:
struct AnyView: View {
var body: some View {
Button("Show Toast") {
showToast(
title: "Operation Successful",
subtitle: "Data has been saved",
type: .success
)
}
}
}
When using a component that displays Toasts inside a modal view (like .sheet
or .fullScreenCover
), you need to add the .wsToast()
modifier to that component:
// Component A that displays Component B in a modal
struct ComponentA: View {
@State private var showModal = false
var body: some View {
Button("Show Modal") {
showModal = true
}
.sheet(isPresented: $showModal) {
ComponentB() // ComponentB already has .wsToast() applied
}
.wsToast() // For toasts in ComponentA
}
}
// Component B that shows toasts inside a modal
struct ComponentB: View {
var body: some View {
VStack {
Text("Modal Content")
Button("Show Toast in Modal") {
showToast(title: "Toast in Modal", type: .info)
}
}
.wsToast() // Required to show toasts in this modal component
}
}
// Success notification
showToast(
title: "Upload Successful",
type: .success
)
// Error notification
showToast(
title: "Connection Failed",
subtitle: "Please check your network connection",
type: .error
)
// Warning notification
showToast(
title: "Low Battery",
type: .warning
)
// Info notification
showToast(
title: "New Message",
type: .info
)
// Custom icon
showToast(
title: "Music Added",
image: "music.note",
type: .success
)
You can use the role
parameter to display toasts with predefined icons for common Apple devices and actions. When a role is specified, it takes precedence over the type
parameter for icon selection.
// Using a role for AirPods
showToast(
title: "AirPods Connected",
role: .airpods
)
// Using a role for Apple Watch
showToast(
title: "Apple Watch Synced",
role: .appleWatch
)
// Using a role for download
showToast(
title: "Download Complete",
role: .download
)
// Available roles include:
// .download, .search, .airpods, .appleWatch, .printer,
// .homePod, .bad, .appleTv, .airPodMax, .safari
If you have any questions or suggestions, please submit an Issue.