Beautiful toast notifications with flexible view settings.
Going to Xcode File
> Add Packages...
and add the repository by giving the URL
https://github.com/bullinnyc/Toast
Enjoy!
import Toast
@main
struct MyApp: App {
@StateObject private var toast = Toast()
var body: some Scene {
WindowGroup {
MyView()
.environmentObject(toast)
}
}
}
struct MyView: View {
@EnvironmentObject private var toast: Toast
var body: some View {
Button(
"Show toast",
action: {
// Show toast.
toast.show(
title: "Some title",
message: "Some message"
) { isShowToast in
print(isShowToast)
}
}
)
}
}
#Preview {
MyView()
// Adding an object to a view’s environment makes
// the toast available to preview.
.environmentObject(Toast())
}
struct MyView: View {
@EnvironmentObject private var toast: Toast
var body: some View {
// ...
}
// Cancel next toasts.
private func cancelNextToasts() {
toast.cancelNextToasts()
}
// Cancel all toasts.
private func cancelAllToasts() {
toast.cancelAllToasts()
}
}
Note: Default style
is set to space
.
You can try other styles or create your own style with extension ToastStyle
.
.space
.mars
- iOS 15.0 +
- Xcode 15.0 +
- SwiftUI
- Toast is distributed under the MIT License.