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.
Top Toast | Middle Toast | Bottom Toast |
---|---|---|
Gesture Toast | Rotating Toast | TextField Toast |
Sheet Toast | ||
- πͺ½ 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()
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.git
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
)
}
}
}
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)
- iOS 13.0+
- macOS 11.0+
- Swift 6.0+
Contributions are welcome! Please feel free to Fork and submit a Pull Request.
- Keyboard avoidance is not fully functional
- FocusState doesn't work as expected with pulling up the keyboard
- Different behavior on device and simulators
Michael Ellis
- Inspired by various toast notification implementations
- Additional thanks to these sources
This project is licensed under the MIT License - see the LICENSE.txt file for details.