From any view controller, a Loaf can be presented by calling:
Loaf("Message goes here", sender: self).show()
Which will result in:
Bellow, I will discuss how to further customize your Loaf!
I've provided an example project to showcase uses of Loaf! Simply clone this repo, and open LoafExamples.xcodeproj
. From here you can see and experiment custom Loaf styles in Examples.swift
Loaf 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 Loaf, call it like so:
Loaf("This is a success loaf", state: .success, sender: self).show()
Loaf 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 Loaf styles!
Colors and icon | Right icon alignment | No icon |
---|---|---|
![]() |
![]() |
![]() |
All of these properties are specified as part of custom state, like so:
Loaf("Switched to light mode", state: .custom(.init(backgroundColor: .black, icon: UIImage(named: "moon"))), sender: self).show()
Loaf 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 | Mix |
---|---|
![]() |
![]() |
These are specified in the function signature, like so:
Loaf("Loaf message", presentingDirection: .left, dismissingDirection: .vertical, sender: self).show()
Toasts are typically presented at the bottom of the screen, but Loaf allows you to also present them at the top of the screen. Here is an example of a Loaf being presented at the top of the view:
This is also specified in the function signature, like so:
Loaf("Loaf message", location: .top, sender: self).show()
Specify the presentation duration. When presenting a Loaf 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.
0.3.0
:
A completion handler can be specified in the Loaf show()
function signature. This block will be called when the dismissal animation is completed. Here is an example of using a completion handler, now named onTap
:
Loaf(example.rawValue, sender: self).show {
print("Dismissed!")
}
Loaf is on Cocoapods! After setting up Cocoapods in your project, simply add the folowing to your Podfile:
pod 'Loaf'
then run pod install
from the directory containing the Podfile!
Don't forget to include import Loaf
in every file you'd like to use Loaf
- Swift 4.2
- iOS 11.0
Pull requests, feature requests and bug reports are welcome π
Thanks to @kirkbyo for helping me through the tough parts of this πͺ
Made with β€οΈ in π¨π¦ by Mat Schmid