Present custom alerts, toasts, and banners in pure-SwiftUI.
Toasty allows you to design and present unobtrusive status updates inside your app (colloquially known as "toasts" ) that don't require any user action to dismiss or to validate. Common uses include things like: Message Sent
, Poor Network Connection
, Profile Updated
, Logged In/Out
, Favorited
, Loading
, etc.
- Built entirely in SwiftUI
- Includes 3 out of the box display modes:
Alert
: Pops a square toast in the center of the screen (similar to the Apple Music "Added to Library" overlays)HUD
: Drops a rounded capsule from the top of the screen (similar to the system "AirPods connected" overlays)Banner
: Slides a rounded rectangular banner up from the bottom of the screen (similar to the Instagram "No Connection" overlays)
- Includes 6 flexible styling options:
Complete
: an animated checkmarkError
: an animated xmarkSystemImage
: a system iconImage
: your own custom imageLoading
: a spinning progress indicatorRegular
: just good ol' title and subtitle only
- Supports Light & Dark Mode
- Works with any kind of view builder.
- Localization support
- Customize just about any aspect of your toast (font, color, background, etc.)
The Swift Package Manager is a tool for managing the distribution of Swift code. Itβs integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
To integrate Toasty
into your Xcode project add the following dependency
https://github.com/nenosllc/Toasty.git, :branch="master"
If you prefer not to use any of dependency managers, you can integrate AlertToast
into your project manually. Put Sources/AlertToast
folder in your Xcode project. Make sure to enable Copy items if needed
and Create groups
.
- iOS 14.0+ | macOS 12+
- Xcode 13.0+ | Swift 5.5+
import Toasty
everywhere you would like to use Toasty
. Then, use the .toast
view modifier:
Parameters:
isPresenting
[required]: assign aBinding<Bool>
to show or dismiss alert.duration
: default is 2, set 0 to disable auto dismiss.tapToDismiss
: default istrue
, setfalse
to disable.alert
[required]: expects anAlertToast
view.
import SwiftUI
import Toasty
struct ContentView: View {
@State private var showToast = false
var body: some View {
VStack {
Button("Show Toast") {
showToast.toggle()
}
}
.toast(isPresenting: $showToast) {
AlertToast(type: .regular, title: "Message Sent!")
}
}
}
.toast(isPresenting: $showAlert, duration: 2, tapToDismiss: true, alert: {
// AlertToast goes here
}, onTap: {
// If tapToDismiss is true, onTap is called and then dismiss the alert.
}, completion: {
// Completion block called after dismiss
})
AlertToast(
displayMode: DisplayMode,
type: AlertType,
title: Optional(String),
subTitle: Optional(String),
style: Optional(AlertStyle)
)
AlertStyle(
backgroundColor: Color?,
titleColor: Color?,
subTitleColor: Color?,
titleFont: Font?,
subTitleFont: Font?
)
- Regular: text only (title and subtitle).
- Complete: animated checkmark.
- Error: animated xmark.
- System Image: system image from
SFSymbols
. - Image: named image from your assets catalog.
- Loading: circular progress indicator.
Alert dialog view modifier (with default settings)
.toast(isPresenting: Binding<Bool>, duration: Double = 2, tapToDismiss: true, alert: () -> AlertToast , onTap: () -> (), completion: () -> () )
Simple Text Alert:
AlertToast(type: .regular, title: Optional(String), subTitle: Optional(String))
Complete / Error Alert:
AlertToast(type: .complete(Color)/.error(Color), title: Optional(String), subTitle: Optional(String))
System Image Alert:
AlertToast(type: .systemImage(String, Color), title: Optional(String), subTitle: Optional(String))
Image Alert:
AlertToast(type: .image(String), title: Optional(String), subTitle: Optional(String))
Loading Alert:
// When using loading, duration won't auto dismiss and tapToDismiss is set to false
AlertToast(type: .loading, title: Optional(String), subTitle: Optional(String))
Note: you can add multiple .toast
modifiers to a single view.
You can take a look at the original article which inspired this library on Medium: How to present a toast alert in SwiftUI. There is additional inline documentation you can refer to as well. From the Xcode menu, select Product > Build Documentation.
All issue reports, feature requests, pull requests and GitHub stars are welcomed and much appreciated.
This fork is based on the original library from Elai Zuberman.
Toasty
is available under the MIT license. See the LICENSE file for more info.