From any view controller, a Toast can be presented by calling:
Toast("Your message", source: self).show()
So, I will discuss how to customize your Toast!
I've provided an example project to showcase uses of Toast! Simply clone this repo, and open Example.xcodeproj
. From here you can see and experiment custom Toast styles in ViewController.swift
Toast comes with 4 basic style out of the box.
Success | Error |
---|---|
Warning | Info |
---|---|
These styles can be specified in the style
property.
For instance, to use Success
styled Toast, call it like so:
Toast("This is a success toast", state: .success, source: self).show()
Toast allows you to specify a custom style! This will let you set the colors, font, icon. and icon alignment. Here are some examples of custom Toast styles!
Colors and icon | Right icon alignment | No icon |
---|---|---|
All of these properties are specified as part of custom state, like so:
Toast(
"Toast message",
state: .custom(
.init(
backgroundColor: .black,
accessory: UIImage(named: "image")
)
),
source: self
).show()
Toast allows you to specify the presenting and dismissing direction. The presenting direction is independant from the dismissal direction. Here are some examples:
Vertical | Left |
---|---|
Right | Combo |
---|---|
These are specified in the function signature, like so:
Toast(
"Toast message",
presentingDirection: .left,
dismissingDirection: .vertical,
source: self
).show()
Specify the presentation duration. When presenting a Toast with .show()
, a presentation duration can be specified. The default value is 4s, but there are presets for 2s and 8s. This is done by using .show(.short)
for 2s, or .show(.long)
for 8s. A custom duration can also be specified with .show(.custom(x))
, where x represents the duration in seconds.
A Toast's width can be specified via the Style
component. The width can be specifed as a fixed size (i.e. 280px) or as a percentage of the screen's width. (i.e. 0.8
-> 87%). Here is some example usage:
Toast(
"Toast message",
state: .custom(
.init(
backgroundColor: .black,
width: .screenPercentage(0.87)
)
),
source: self
).show()
Toast is on Cocoapods! After setting up Cocoapods in your project, simply add the folowing to your Podfile: pod 'swifty-toast'
then run pod install
from the directory containing the Podfile!
Don't forget to include import Toast
in every file you'd like to use Toast
- Swift 5.3+
- iOS 12.0+