8000 GitHub - michael94ellis/ToastWindow: A SwiftUI oriented Toast library designed to utilize UIKit's UIWindow for always on top Toast views
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

michael94ellis/ToastWindow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ToastWindow

Swift Package Manager Platform Platform Swift

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 modern look using any SwiftUI view you pass in.

Control your toasts entirely with SwiftUI (see demo project)

Top Toast Middle Toast Bottom Toast
Gesture Toast Rotating Toast TextField Toast
Sheet Toast

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(
                content: Text("Hello, World!")
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10),
                duration: 2.0
            )
        }
    }
}

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(content: SuccessToast(),
                       duration: 3.0)

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

Author

Michael Ellis

Acknowledgments

License

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

About

A SwiftUI oriented Toast library designed to utilize UIKit's UIWindow for always on top Toast views

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0