AlertCenter is a flexible and customizable component for displaying animated alerts in your iOS app. It gives you full control over the animation and the view used to present your alerts, making it a powerful tool for enhancing user experience in your app.
AlertCenter was originally created as a replacement for MDCSnackBar, which Google discontinued in 2021. While it can serve as a drop-in replacement for SnackBars using the provided ToastAlertCenter
class, AlertCenter goes further by allowing you to create your own custom animated alerts and even design custom animations.
- In Xcode, select “File” → “Add Packages...”
- Enter https://github.com/chrsp/AlertCenter.git
or you can add the following dependency to your Package.swift
:
.package(url: "https://github.com/chrsp/AlertCenter.git", from: "1.0.0")
The heart of the AlertCenter component lies in the AlertCenter
protocol, which serves as the foundation for managing how the center displays alerts. This protocol offers several key functionalities:
public protocol AlertCenter: AnyObject {
// 1
associatedtype AlertView: AnimatedAlert
// 2
var animator: AlertAnimator { get set }
// 3
var alertQueue: [AlertView] { get set }
// 4
func display(message: String, time: TimeInterval, onView: UIView?, action: ToastAction?)
}
-
AlertView Customization: You can tailor the appearance of the alert views by associating them with your preferred
AlertView
conforming to theAnimatedAlert
protocol. -
Animation Control: Customize the animations applied to the alerts through the
AlertAnimator
property. -
Alert Queue Management: Maintain a queue of alerts to ensure they are displayed in an orderly fashion.
-
Display Alerts: Request the center to display an alert with specific parameters, such as the message, display duration, target view, and an optional action.
AlertCenter provides ready-made AlertCenter
types like ToastAlertCenter
, which emulates the Material Snackbar's appearance. Nevertheless, you have the freedom to create your own custom animations, views, or even entirely new AlertCenter
implementations.
To get started, include the library in your project by adding:
import AlertCenter
Then, initialize your preferred AlertCenter
instance, for example:
var alertCenter = ToastAlertCenter<MDCSnackbarToastAlert>()
Finally, choose how you want to present your Animated Alert:
-
Display on a Specific View:
self.alertCenter.display(message: "Displayed on this view!", time: 1.0, onView: self.view)
-
Display on the Window:
self.alertCenter.display(message: "Displayed on the window!", time: 1.0, onView: nil)
-
Display with an Action Button:
self.alertCenter.display(message: "Displayed with Action", time: 2.0, onView: self.view, action: action)
-
Display Action on the Window:
self.alertCenter.display(message: "Displayed with Action", time: 2.0, onView: nil, action: action)
AlertCenter is available under the MIT license. See the LICENSE file for more information.
If you have any questions, encounter issues, or would like to make suggestions, please don't hesitate to send me an email or leave me a message.
I hope you find AlertCenter a valuable addition to your iOS development toolkit. Happy coding! 😉