Contents

michael94ellis/toastwindow

A lightweight Swift package for displaying SwiftUI View's as toast notifications in iOS applications leveraging UIKit's UIWindow. ToastWindow creates a secondary window to display toast notifications, ensuring they appear above all other content while maintaining a clean and mode

Features

  • πŸͺ½ SwiftUI Toast Views
  • πŸ–ŒοΈ Fully customizable using SwiftUI Modifiers

- πŸ–ΌοΈ Icons & Images – Enables adding symbols or images in the toast message - 🎨 Themes & Styling – Allows color, typography, shadow, and rounded corner customization - 🎭 Customizable Animations – Build animations using SwiftUI Modifiers - βœ‹ Gesture Handling – Enable touch and swipe gestures, such as dismissing by tap or swipe - πŸ“Œ Positioning Control - Use SwiftUI to position your content - πŸ”„ Device Rotation - Position will update when the device rotates - πŸ“ Input Fields - Use TextFields and other inputs

  • πŸ” Displays on top of everything including sheets from the .sheet SwiftUI modifier
  • πŸ”„ Built-in Window management - Prevent memory leaks
  • πŸ”’ Thread Safety - Ensures UI updates occur on the main thread
  • ⏱️ Customizable Duration - Be sure to include animation duration in duration passed to .showToast()

Installation

Swift Package Manager

Add the following dependency to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/michael94ellis/ToastWindow.git", from: "1.0.0")
]

Or add it directly in Xcode:

  1. Go to File > Add Packages...
  2. Enter the repository URL below and then click Add Package
https://github.com/michael94ellis/ToastWindow.git

Usage

Basic Usage

import SwiftUI
import ToastWindow

struct ContentView: View {
    @Environment(\.toastManager) private var toastManager
    
    var body: some View {
        Button("Show Toast") {
            toastManager.showToast(duration: 2.0) { 
                Text("Hello, World!")
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }
        }
    }
}

Custom Toast Content

struct SuccessToast: View {
    
    @State private var isVisible = false
    
    var body: some View {
        Text("Account Created Successfully")
            .multilineTextAlignment(.center)
            .foregroundStyle(.white)
            .font(.title.weight(.semibold))
            .frame(width: 250, height: 150) 
            .background(Color(.systemGreen))
            .cornerRadius(25) 
            .shadow(radius: 5)
            .opacity(isVisible ? 1 : 0)
            .scaleEffect(isVisible ? 1 : 0.2)
            .onAppear {
                withAnimation(.easeInOut(duration: 0.5)) {
                    isVisible = true
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                    withAnimation(.easeInOut(duration: 0.5)) {
                        isVisible = false
                    }
                }
            }
    }
}

// Usage:
toastManager.showToast(duration: 3.0) {
    SuccessToast()
}

Requirements

  • iOS 13.0+
  • macOS 11.0+
  • Swift 6.0+

Contributing

Contributions are welcome! Please feel free to Fork and submit a Pull Request.

Known Issues

Help Wanted!

  • Keyboard avoidance is not fully functional
  • FocusState doesn't work as expected with pulling up the keyboard

- Different behavior on device and simulators

Acknowledgments

  • Inspired by various toast notification implementations
  • Additional thanks to these sources

- https://stackoverflow.com/questions/14740921/passing-touches-between-stacked-uiwindows - https://www.fivestars.blog/articles/swiftui-windows/

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

Package Metadata

Repository: michael94ellis/toastwindow

Default branch: main

README: README.md