A toaster library for iOS written in Swift.
- π€ Android-like Toast.
- π¦ Ready-to-use right out of the box.
- βοΈ Easy to customize.
- π¦ Swift package manager supported.
- β¨οΈ Not blocked by the keyboard.
- π iOS 11 or newer is support.
studio_video_1679933979987464.MP4
To integrate Toaster into your Xcode project using Swift Package Manager, add it to the dependencies value of your Package.swift:
dependencies: [
.package(url: "https://github.com/bluesky335/Toaster.git", .upToNextMajor(from: "0.2.0"))
]
or using Xcode swift package manager:
then add 'Toaster' in to 'Frameworks,Libraries,and Embedded Content':
Toast(text:"π o(οΏ£β½οΏ£)ο½ ").show()
// customize toast style
Toast.successStyle = .init(backgroundColor: .systemGreen.withAlphaComponent(0.8))
Toast.errorStyle = .init(backgroundColor: .systemRed.withAlphaComponent(0.8))
Toast.warningStyle = .init(backgroundColor: .systemYellow.withAlphaComponent(0.8))
Toast.defaultStyle = .init()
// customize toast position layout or fixedWidth
ToastCenter.default.toastAnimator = ToastAnimatter(position: .top, layout: .stack, fixedWidth: true)
-
implement you own ToastType
Optional step, you can use the default implementation:
Toast
public struct MyToast: ToastType { public var duration: ToastDuration public var content: String }
-
implement a ToastViewProviderType for your ToastType
public final class MyToastViewProvider: ToastViewProviderType<MyToast> { @MainActor override public func viewForToast(toast: MyToast) -> UIView { let view = UIView() /* .... Create a view and set it via the "toast" parameter. This view will be displayed by toast center. .... */ return view } }
-
register your ToastViewProviderType into toast center
// If this type of toast is already registered, this will replace it. ToastCenter.default.register(toastType: MyToast.self, with: MyToastViewProvider())
-
show your toast
MyToast(duration:.short, content: "toast text").show()
you can also customize the toast animation by implement your own ToastAnimatterType and set it to ToastCenter.default.toastAnimator
replacing the default ToastAnimatter.
ToastCenter.default
display the toast view in the first UIWindowScene connected to the app.
you can create ToastCenter for diffrent window scenes by ToastCenter(windowScene: scene)
.
then using Toast(text:"toast text").show(in: center)
to show toast.