A Toast View for SwiftUI
Disclaimer: Most of the codes aren't written by me I found it on a medium post as I was reseaching how to write a toast for Swift.
In addition to the toast, I have added an ObservableObject which can will help to coordinate all the toast. You can call showToast to add it to the list.
I would advice you to add it to your root view controller
Got to File | Swift Packages | Add Package Dependency... and paste the github link. Choose main branch.
| Type | Dark | Light |
|---|---|---|
| boot | ![]() |
![]() |
| fancy | ![]() |
![]() |
| top | middle | bottom | |
|---|---|---|---|
| position | ![]() |
![]() |
![]() |
import SwiftToastimport SwiftToast
struct MenuView: View {
@ObservedObject var toastCoordinator = ToastCoordinator()
var body: some View {
VStack {
Text("Hello World")
}
.modifier(ToastModifier())
.environmentObject(toastCoordinator)
}
}import SwiftToast
struct MenuViewTest: View {
@EnvironmentObject var toastCoordinator: ToastCoordinator
var body: some View {
VStack {
Button("no icon, no button") {
toastCoordinator.showToast(Toast(type: .boot,
title: "test",
message: "test",
duration: 1))
}
}
}
}SwiftToast is distributed under the MIT license. See LICENSE for details.






