AlertToast-SwiftUI

repository·master·Indexed 25 days ago

https://github.com/elai950/alerttoast

A SwiftUI library for presenting Apple-like alerts and toasts, including HUDs, banners, and center alerts. It supports various alert types such as .regular, .complete, .error, .systemImage, .image, and .loading, and provides a .toast view modifier for controlling visibility and auto-dismissal.

Tokens
1.1K
Snippets
4
Records
7
Agent score
32%

What's inside AlertToast

  1. Configure AlertToast display modes

    master

    You can control where and how the toast appears using the displayMode parameter in the AlertToast initializer:

    • .alert: Pops up in the center of the screen (Default).
    • .hud: Drops down from the top of the screen.
    • .banner(.slide): Slides/pops up from the bottom of the screen.
  2. Install AlertToast manually

    master
    To install manually without a dependency manager, add the Sources/AlertToast folder directly to your Xcode project. Ensure that you enable Copy items if needed and Create groups during the import process.
  3. Install AlertToast via Swift Package Manager

    master

    To integrate AlertToast using Swift Package Manager in Xcode 12+, navigate to File > Swift Packages > Add Package Dependency... and use the following URL:

    • Repository URL: https://github.com/elai950/AlertToast.git
    • Dependency Rule: :branch="master"
    https://github.com/elai950/AlertToast.git, :branch="master"
  4. Use the .toast view modifier

    master

    To display an alert, first import AlertToast. Then, apply the .toast view modifier to a view. The modifier requires a Binding<Bool> to control visibility and an alert closure that returns an AlertToast instance.

    Parameters:

    • isPresenting: (Required) A Binding<Bool> to show or dismiss the alert.
    • duration: (Optional) Default is 2. Set to 0 to disable auto-dismiss.
    • tapToDismiss: (Optional) Default is true. Set to false to disable.
    • alert: (Required) A closure returning an AlertToast object.
    • onTap: (Optional) A closure called when the user taps the toast.
    • completion: (Optional) A closure called after the toast is dismissed.
    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){
                // Default displayMode is .alert (center pop-up)
                AlertToast(type: .regular, title: "Message Sent!")
                
                // To show from top (HUD mode):
                // AlertToast(displayMode: .hud, type: .regular, title: "Message Sent!")
                
                // To slide from bottom (Banner mode):
                // AlertToast(displayMode: .banner(.slide), type: .regular, title: "Message Sent!")
            }
        }
    }
  5. Customize AlertToast appearance with AlertStyle

    master

    Use the style parameter in AlertToast to customize fonts and colors using AlertStyle:

    AlertToast(
        type: .regular, 
        title: "Custom Style", 
        style: AlertStyle(
            backgroundColor: .blue, 
            titleColor: .white, 
            subTitleColor: .lightGray, 
            titleFont: .headline, 
            subTitleFont: .caption
        )
    )
    AlertStyle(backgroundColor: Color?,
                titleColor: Color?,
                subTitleColor: Color?,
                titleFont: Font?,
                subTitleFont: Font?)
  6. Configure AlertToast types

    master

    The type parameter determines the visual content of the toast. Available AlertType options include:

    • .regular: Displays only text (Title and Subtitle).
    • .complete(Color): Displays an animated checkmark with a specific color.
    • .error(Color): Displays an animated xmark with a specific color.
    • .systemImage(String, Color): Displays an image from SFSymbols with a specific color.
    • .image(String): Displays an image from your project's Assets.
    • .loading: Displays an Activity Indicator (Spinner). Note: For .loading, duration will not auto-dismiss and tapToDismiss is automatically set to false.