ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - BastiaanJansen/toast-swift: Customizable Swift Toast view built with UIKit. 🍞
Customizable Swift Toast view built with UIKit. 🍞. Contribute to BastiaanJansen/toast-swift development by creating an account on GitHub.
Visit Site

GitHub - BastiaanJansen/toast-swift: Customizable Swift Toast view built with UIKit. 🍞

GitHub - BastiaanJansen/toast-swift: Customizable Swift Toast view built with UIKit. 🍞

Toast-Swift

Codacy Badge

A Swift Toast view - iOS 14 style - built with UIKit. 🍞

Installation

Swift Package Manager

You can use The Swift Package Manager to install Toast-Swift by adding the description to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/BastiaanJansen/toast-swift", from: "2.1.2")
]

CocoaPods

pod "ToastViewSwift"

Usage

To create a simple text based toast:

let toast = Toast.text("Safari pasted from Notes")
toast.show()

Or add a subtitle:

let toast = Toast.text("Safari pasted from Notes", subtitle: "A few seconds ago")
toast.show()

And if you want to use your own font(NSAttributedString is supported):

let attributes = [
    NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!, 
    NSAttributedStringKey.foregroundColor: UIColor.black
]
let attributedString  = NSMutableAttributedString(string: "Safari pasted from Notes" , attributes: attributes)
let toast = Toast.text(attributedString)
toast.show()

If you want to add an icon, use the default method to construct a toast:

let toast = Toast.default(
    image: UIImage(systemName: "airpodspro")!,
    title: "Airpods Pro",
    subtitle: "Connected"
)
toast.show()

Want to use a different layout, but still use the Apple style? Create your own view and inject it into the AppleToastView class when creating a custom toast:

let customView: UIView = // Custom view

let appleToastView = AppleToastView(child: customView)

let toast = Toast.custom(view: appleToastView)
toast.show()

The show method accepts several optional parameters. haptic of type UINotificationFeedbackGenerator.FeedbackType to use haptics and after of type TimeInterval to show the toast after a certain amount of time:

toast.show(haptic: .success, after: 1)

Configuration options

The text, default and custom methods support custom configuration options. The following options are available:

Name Description Type Default
direction Where the toast will be shown. .bottom or .up .up
dismissBy Choose when the toast dismisses. Dismissable [.time, .swipe]
animationTime Duration of the show and close animation in seconds. TimeInterval 0.2
enteringAnimation The type of animation that will be used when toast is showing .slide, .fade, .scaleAndSlide, .scale and .custom .default
exitingAnimation The type of animation that will be used when toast is exiting .slide, .fade, .scaleAndSlide, .scale and .custom .default
attachTo The view which the toast view will be attached to. UIView nil
let config = ToastConfiguration(
    direction: .top,
    dismissBy: [.time(time: 4.0), .swipe(direction: .natural), .longPress],
    animationTime: 0.2
)

let toast = toast.text("Safari pasted from Notes", config: config)

Custom entering/exiting animations

self.toast = Toast.text(
            "Safari pasted from Noted",
            config: .init(
                direction: .bottom,
                enteringAnimation: .fade(alphaValue: 0.5),
                exitingAnimation: .slide(x: 0, y: 100))
            ).show()

The above configuration will show a toast that will appear on screen with an animation of fade-in. And then when exiting will go down and disapear.

self.toast = Toast.text(
            "Safari pasted from Noted",
            config: .init(
                direction: .bottom,
                enteringAnimation: .scale(scaleX: 0.6, scaleY: 0.6),
                exitingAnimation: .default
            ).show()

The above configuration will show a toast that will appear on screen with scaling up animation from 0.6 to 1.0. And then when exiting will use our default animation (which is scaleAndSlide)

For more on animation see the Toast.AnimationType enum.

Custom toast view

Don't like the default Apple'ish style? No problem, it is also possible to use a custom toast view with the custom method. Firstly, create a class that confirms to the ToastView protocol:

class CustomToastView : UIView, ToastView {
    private let text: String

    public init(text: String) {
        self.text = text
    }

    func createView(for toast: Toast) {
        // View is added to superview, create and style layout and add constraints
    }
}

Use your custom view with the custom construct method on Toast:

let customToastView: ToastView = CustomToastView(text: "Safari pasted from Notes")

let toast = Toast.custom(view: customToastView)
toast.show()

Queues

To show toasts after each other, use the ToastQueue class:

let toast1 = Toast.text("Notification 1")
let toast2 = Toast.text("Notification 2")
let toast3 = Toast.text("Notification 3")

let queue = ToastQueue([toast1, toast2, toast3])

queue.show()

Delegates

Below delegate functions are optional to implement when implementing ToastDelegate.

extension MyViewController: ToastDelegate {
    func willShowToast(_ toast: Toast) {
        print("Toast will be shown after this")
    }

    func didShowToast(_ toast: Toast) {
        print("Toast was shown")
    }

    func willCloseToast(_ toast: Toast) {
        print("Toast will be closed after this")
    }

    func didCloseToast(_ toast: Toast) {
        print("Toast was closed (either automatically, dismissed by user or programmatically)")
    }
}

Licence

Toast-Swift is available under the MIT licence. See the LICENCE for more info.

Stargazers repo roster for @BastiaanJansen/Toast-Swift

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory