Swiftpack.co - sanzaru/SimpleToast as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
sanzaru/SimpleToast
SimpleToast is a simple, lightweight and easy to use library to show toasts / pop-ups inside your iOS application in SwiftUI.
.package(url: "https://github.com/sanzaru/SimpleToast.git", from: "0.5.0")

SimpleToast for SwiftUI

Build Status

SimpleToast is a simple, lightweight and easy to use library to show toasts / popup notifications inside your iOS or MacOS application in SwiftUI.

You decide the content, the library takes care about the rest.

⚠️ Note: The current version is still in an early development stage. There can and will be breaking changes in version updates.

Table of contents

Features:

  • Custom toast content support: You can show whatever you want inside the toast.
  • Custom positioning: Place the toast where you want it to be.
  • Timeout functionality: You decide if and when the toast should disappear.
  • Callback functionality: Run code when the toast disappeared.
  • Multiple, customizable animations

Demo

Modifier Demo
.slide
.fade
.scale
.skew

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/sanzaru/SimpleToast.git", from: "0.0.1")
]

Manual installation

ℹ️ Manual installation is not advised anymore. Please use the package version instead.

Usage:

Attach via boolean

Simply attach the toast to a view and show it via binding to Bool with a 5 sec. delay:

import SwiftUI
import SimpleToast

struct ToastTestView: View {
    @State var showToast: Bool = false

    private let toastOptions = SimpleToastOptions(
        hideAfter: 5
    )

    VStack(spacing: 20) {
        Button("Show toast") {
            withAnimation {
                showToast.toggle()
            }
        }
    }
    .simpleToast(isShowing: $showToast, options: toastOptions) {
        HStack {
            Image(systemName: "exclamationmark.triangle")
            Text("This is some simple toast message.")
        }
        .padding()
        .background(Color.red.opacity(0.8))
        .foregroundColor(Color.white)
        .cornerRadius(10)
    }
}

Note: The toast respects the frame of the view it is attached to. Make sure the view has enough room to render the toast. Preferably the toast should be attached to the most outer view or the navigation view, if available.

Attach via optional object

You can trigger the toast via an instance to an optional, which conforms to the protocol Identifiable. If the value is non-nil the toast will be shown. The following example is based on the previous one and also shows the toast for 5 seconds, but this time based on a value on an item.

import SwiftUI
import SimpleToast

struct ToastTestView: View {
    @State var showToast: DummyItem? = nil

    private struct DummyItem: Identifiable {
        var foo: String = "Bar"
    }

    private let toastOptions = SimpleToastOptions(
        hideAfter: 5
    )

    VStack(spacing: 20) {
        Button("Show toast") {
            withAnimation {
                // Toggle the item
                showToast = showToast == nil ? DummyItem() : nil
            }
        }
    }
    .simpleToast(item: $showToast, options: toastOptions) {
        HStack {
            Image(systemName: "exclamationmark.triangle")
            Text("This is some simple toast message.")
        }
        .padding()
        .background(Color.red.opacity(0.8))
        .foregroundColor(Color.white)
        .cornerRadius(10)
    }
}

 
ℹ️ This functionality is similar to the one of the SwiftUI sheet(item:onDismiss:content:)
 

Run code after dismiss

To run custom code after the toast disappeared you just simply have to pass a function to the onDismiss parameter:

import SwiftUI
import SimpleToast

struct ToastTestView: View {
    @State var showToast: Bool = false

    private let toastOptions = SimpleToastOptions(
        hideAfter: 5
    )

    VStack(spacing: 20) {
        Button("Show toast") {
            withAnimation {
                showToast.toggle()
            }
        }
    }
    .simpleToast(isShowing: $showToast, options: toastOptions, onDismiss: onToastComplete) {
        HStack {
            Image(systemName: "exclamationmark.triangle")
            Text("This is some simple toast message.")
        }
        .padding()
        .background(Color.red.opacity(0.8))
        .foregroundColor(Color.white)
        .cornerRadius(10)
    }

    // This will be called on toast completion
    func onToastComplete() -> Void {
        print("The toast did disappear")
    }
}

Options

The toast can be configured via an optional SimpleToastOptions object. You can simply pass an empty object to configure the toast with default values.

 
📌 All parameters inside the options are optional. If not set, the default value will be taken.
 

Option Description Default
alignment Defines the alignment of the toast. See https://developer.apple.com/documentation/swiftui/alignment for more information. .top
hideAfter Defines when the toast disappears. If nil is given the toast won't disappear. nil
showBackdrop Defines if the toast is rendered over a backdrop. true
backdropColor Defines the backdrop color Color.white.opacity(0.9)
animation Defines the animation type. See https://developer.apple.com/documentation/swiftui/animation for more information. .linear
modifierType Defines the type of toast animation. Possible values(.slide, .fade) .fade

The struct has the following signature:

public struct SimpleToastOptions {
    var alignment: Alignment = .top
    var hideAfter: TimeInterval? = nil
    var showBackdrop: Bool? = true
    var backdropColor: Color = Color.white.opacity(0.9)
    var animation: Animation = .linear
    var modifierType: SimpleToastModifierType = .fade
}

Changelog

See CHANGELOG.md for a overview of changes

GitHub

link
Stars: 42
Last commit: 5 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

0.5.0
5 days ago

Bugfix release for iOS 13 compatibility:

  • Bugfix for #10

ℹ️ This release changed the internal logic to display the toast. It should just work fine, with no changes required.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics