A library for SwiftUI to create custom alerts with different appearances and animations.

What's New

More Customization

  • Custom rounded corner radius can be specified in themes.


Custom alerts in SwiftUI made easy

AlertX is a library for SwiftUI projects to have custom alerts that can be implemented in a very easy and simple way, just like implementing built-in system alerts.

A Quick Example

    import SwiftUI
    import AlertX
                Button(action: {
                }, label: {
                    Text("Show AlertX")
                }).alertX(isPresented: $showAlertX, content: {
                    AlertX(title: Text("AlertX Title"),
                           message: Text("An optional message indicating some action goes here..."),
                           primaryButton: .cancel(),
                           secondaryButton: .default(Text("Done"), action: {
                            // Some action
                           theme: .graphite(withTransparency: true, roundedCorners: true),
                           animation: .classicEffect())

Example AlertX Screenshot


Working and Installation

AlertX is a Swift Package and can be installed just like installing any other Swift Package libraries.

  1. In your Xcode project, navigate to File > Swift Packages > Add Package Dependency.
  2. In the package repository URL, enter this:

One awesome thing about AlertX is that the implementation of it is much similar to the implementation of system's built in alerts. Once AlertX is installed and imported, a new instance method alertX(isPresented: Binding<Bool>, content: () -> AlertX) can be used with any SwiftUI View, similar to the built-in method.


The appearance of AlertX is customizable with themes. There are a bunch of pre-defined themes already included, but you can also have your very own theme.

All the themes of AlertX are of type AlertX.Theme. There are static methods for each pre-defined theme. Here's an example of how you can apply a theme:

    Button(action: {
    }, label: {
        Text("Show AlertX")
    }).alertX(isPresented: $showAlertX, content: {
        AlertX(title: Text("The Title"), theme: .wine())

For the static methods of all pre-defined themes, you can also pass boolean to enable or disable transparency and rounded corners for alert and buttons. By default, for all themes, transparency is true and rounded corners is set to false. false, roundedCorners: true)

(The above example uses wine theme with transparency false and roundedCorners true)

Here's a list of all pre-defined themes:

graphite (default)

To create a custom theme, use the following method which offers a variety of options:

AlertX.Theme.custom(windowColor: Color,
                   alertTextColor: Color,
                   enableShadow: Bool,
                   enableRoundedCorners: Bool,
                   enableTransparency: Bool,
                   cancelButtonColor: Color,
                   cancelButtonTextColor: Color,
                   defaultButtonColor: Color,
                   defaultButtonTextColor: Color,
                   roundedCornerRadius: CGFloat)


Animations for AlertX can be applied using AlertX.AnimationX. There are various cool pre-defined animations already included. Similar to themes, they can be applied using static method for each animation present inside AlertX.AnimationX.

AlertX with default animation

Here's how you can apply an animation:

AlertX(title: Text("Some Title"), animation: .fadeEffect())

Here's a list of all pre-defined animations:


You can also create your own animations just like you would for any other SwiftUI View using the following method:

AlertX.AnimationX.custom(withTransition: AnyTransition)

Adding More Buttons

Use the initialiser which takes an array of AlertX.Button (buttonStack) to add as many buttons as you want in the alert. Here's an example (body of the closure to be passed in the alertX method):

        let buttons = [
        return AlertX(title: Text("Are you sure about this?"), buttonStack: buttons)

Future Work

  1. Support for text fields.
  2. Even more and better pre-defined themes and animations.


You are most welcome in contributing to this project with either new features (maybe one mentioned from the future work or anything else), refactoring and improving the code or adding new pre-defined themes and animations. Also, feel free to give suggestions and feedbacks.

Created with ❤️ by Neel Makhecha.

Get in touch on Twitter. Visit:


  • Swift Tools 5.1.0
View More Packages from this Author


  • None
Last updated: Sun Jan 15 2023 18:21:29 GMT-0500 (GMT-05:00)