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
.sheetSwiftUI 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:
- Go to File > Add Packages...
- Enter the repository URL below and then click Add Package
https://github.com/michael94ellis/ToastWindow.gitUsage
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