Currently in SwiftUI, the only way to inform the user about some process that finished for example, is by using Alert. Sometimes, you just want to pop a message that tells the user that something completed, or his message was sent. Apple doesn’t provide any other method rather than using Alert even though Apple using all kinds of different pop-ups. The results are poor UX where the user would need to tap “OK/Dismiss” for every little information that he should be notified about.
Alert Toast is an open-source library in Github to use with SwiftUI. It allows you to present popups that don’t need any user action to dismiss or to validate. Some great usage examples: Message Sent, Poor Network Connection, Profile Updated, Logged In/Out, Favorited, Loading and so on…
Built with pure SwiftUI.
3 Display modes: Alert (pop at the center), HUD (drop from the top) and Banner (pop/slide from the bottom).
Complete, ErrorSystemImage, Image, Loading, and Regular (Only Text).
Supports Light & Dark Mode.
Works with any kind of view builder.
Localization support.
Font & Background customization.
If you like the project, don’t forget to put star 🌟.
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate AlertToast into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'AlertToast'
Swift Package Manager
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 AlertToast into your Xcode project using Xcode 12, specify it in File > Swift Packages > Add Package Dependency...:
For Xcode 13, please refer this article to install AlertToast
Manually
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.
🧳 Requirements
iOS 13.0+ | macOS 11+
Xcode 12.0+ | Swift 5+
🛠 Usage
First, add import AlertToast on every swift file you would like to use AlertToast.
Then, use the .toast view modifier:
Parameters:
isPresenting: (MUST) assign a Binding<Bool> to show or dismiss alert.
duration: default is 2, set 0 to disable auto dismiss.
tapToDismiss: default is true, set false to disable.
alert: (MUST) expects AlertToast.
Usage example with regular alert
import AlertToast
import SwiftUI
struct ContentView: View{
@State private var showToast = false
var body: some View{
VStack{
Button("Show Toast"){
showToast.toggle()
}
}
.toast(isPresenting: $showToast){
// `.alert` is the default displayMode
AlertToast(type: .regular, title: "Message Sent!")
//Choose .hud to toast alert from the top of the screen
//AlertToast(displayMode: .hud, type: .regular, title: "Message Sent!")
//Choose .banner to slide/pop alert from the bottom of the screen
//AlertToast(displayMode: .banner(.slide), type: .regular, title: "Message Sent!")
}
}
}
Complete Modifier Example
.toast(isPresenting: $showAlert, duration: 2, tapToDismiss: true, alert: {
//AlertToast goes here
}, onTap: {
//onTap would call either if `tapToDismis` is true/false
//If tapToDismiss is true, onTap would call and then dismis the alert
}, completion: {
//Completion block after dismiss
})
Alert Toast Parameters
AlertToast(displayMode: DisplayMode,
type: AlertType,
title: Optional(String),
subTitle: Optional(String),
style: Optional(AlertStyle))
//This is the available customizations parameters:
AlertStyle(backgroundColor: Color?,
titleColor: Color?,
subTitleColor: Color?,
titleFont: Font?,
subTitleFont: Font?)
//When using loading, duration won't auto dismiss and tapToDismiss is set to false
AlertToast(type: .loading, title: Optional(String), subTitle: Optional(String))
You can add many .toast on a single view.
📖 Article
I wrote an article that contains more usage examples.
AlertToast-SwiftUI
Present Apple-like alert & toast in SwiftUI
🌄 Example
🔭 Overview
Currently in SwiftUI, the only way to inform the user about some process that finished for example, is by using
Alert
. Sometimes, you just want to pop a message that tells the user that something completed, or his message was sent. Apple doesn’t provide any other method rather than usingAlert
even though Apple using all kinds of different pop-ups. The results are poor UX where the user would need to tap “OK/Dismiss” for every little information that he should be notified about.Alert Toast is an open-source library in Github to use with SwiftUI. It allows you to present popups that don’t need any user action to dismiss or to validate. Some great usage examples:
Message Sent
,Poor Network Connection
,Profile Updated
,Logged In/Out
,Favorited
,Loading
and so on…Alert
(pop at the center),HUD
(drop from the top) andBanner
(pop/slide from the bottom).Complete
,Error
SystemImage
,Image
,Loading
, andRegular
(Only Text).If you like the project, don’t forget to
put star 🌟
.₿ Bitcoin donation address:
Navigation
💻 Installation
Cocoapods
AlertToast Cocapods Website
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate
AlertToast
into your Xcode project using CocoaPods, specify it in your Podfile:Swift Package Manager
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
AlertToast
into your Xcode project using Xcode 12, specify it inFile > Swift Packages > Add Package Dependency...
:For Xcode 13, please refer this article to install
AlertToast
Manually
If you prefer not to use any of dependency managers, you can integrate
AlertToast
into your project manually. PutSources/AlertToast
folder in your Xcode project. Make sure to enableCopy items if needed
andCreate groups
.🧳 Requirements
🛠 Usage
First, add
import AlertToast
on everyswift
file you would like to useAlertToast
.Then, use the
.toast
view modifier:Parameters:
isPresenting
: (MUST) 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
: (MUST) expectsAlertToast
.Usage example with regular alert
Complete Modifier Example
Alert Toast Parameters
Available Alert Types:
SFSymbols
.Alert dialog view modifier (with default settings):
Simple Text Alert:
Complete/Error Alert:
System Image Alert:
Image Alert:
Loading Alert:
You can add many
.toast
on a single view.📖 Article
I wrote an article that contains more usage examples.
Medium - How to toast an alert in SwiftUI
👨💻 Contributors
All issue reports, feature requests, pull requests and GitHub stars are welcomed and much appreciated.
✍️ Author
Elai Zuberman
📃 License
AlertToast
is available under the MIT license. See the LICENSE file for more info.