Flash messages – or toasts, in Android parlance – are discrete, non-modal alerts designed to notify users without completely capturing their focus. For example, you might use a flash message to let the user know that new data has finished loading, that a document has been saved, or that a non-critical error has occurred.
Flash.swift makes displaying these kinds of messages easy, and gives you flexibility to custom their appearance and behaviour.
Screenshots
Installation
Flash.swift is available through the Swift Package Manager. To use Flash.swift with SPM, add https://github.com/conmulligan/Flash.swift.git as a dependency.
Getting Started
For an interactive example that you can build and run, check out the Xcode project in the Example directory.
Basic example
Showing a flash message can be as simple as creating a FlashView instance and calling the show() function:
let flash = FlashView(text: "Hello!")
flash.show()
You can also pass an image:
let star = UIImage(systemName: "star.fill")
let flash = FlashView(text: "Hello!", image: star)
flash.show()
By default, flash views are added directly to the active window’s view heirarchy. Adding the flash view direct to the window has some advantages; for example, doing so allows the flash view to survive changes to the view heirarchy, like a view controller being popped from a navigation stack.
If you want to show a flash message in a specific view, pass a UIView instance to the show() method:
flash.show(in: view)
By default, flash messages are visible for 2 seconds. You can change the duration by passing a TimeInterval value:
flash.show(duration: 5) // seconds
If you want the flash message to appear indefinitely, pass a duration of 0:
flash.show(duration: 0)
The flash message will be visible until the user taps on it (if tap-to-dismiss is enabled), or until dismissed programatically:
flash.hide()
Customization
The FlashView.Configuration struct allows you to customize the flash view’s alignment, background color, text color and more. In most cases, the default configuration should be used as a baseline:
var configuration = FlashView.Configuration.defaultConfiguration()
configuration.titleProperties.textColor = .red
You can also create a configuration from scratch using the default initializer:
let configuration = FlashView.Configuration(alignment: .bottom,
playsHaptics: false,
tapToDismiss: false)
Once you have created a configuration, pass it to the flash view’s initializer:
let flash = FlashView(text: "Hello!", configuration: configuration)
flash.show()
If you want to change the configuration for all flash views, use the shared static property:
Flash messages – or toasts, in Android parlance – are discrete, non-modal alerts designed to notify users without completely capturing their focus. For example, you might use a flash message to let the user know that new data has finished loading, that a document has been saved, or that a non-critical error has occurred.
Flash.swift makes displaying these kinds of messages easy, and gives you flexibility to custom their appearance and behaviour.
Screenshots
Installation
Flash.swift is available through the Swift Package Manager. To use Flash.swift with SPM, add
https://github.com/conmulligan/Flash.swift.git
as a dependency.Getting Started
For an interactive example that you can build and run, check out the Xcode project in the
Example
directory.Basic example
Showing a flash message can be as simple as creating a
FlashView
instance and calling theshow()
function:You can also pass an image:
By default, flash views are added directly to the active window’s view heirarchy. Adding the flash view direct to the window has some advantages; for example, doing so allows the flash view to survive changes to the view heirarchy, like a view controller being popped from a navigation stack.
If you want to show a flash message in a specific view, pass a
UIView
instance to theshow()
method:By default, flash messages are visible for 2 seconds. You can change the duration by passing a
TimeInterval
value:If you want the flash message to appear indefinitely, pass a duration of
0
:The flash message will be visible until the user taps on it (if tap-to-dismiss is enabled), or until dismissed programatically:
Customization
The
FlashView.Configuration
struct allows you to customize the flash view’s alignment, background color, text color and more. In most cases, the default configuration should be used as a baseline:You can also create a configuration from scratch using the default initializer:
Once you have created a configuration, pass it to the flash view’s initializer:
If you want to change the configuration for all flash views, use the
shared
static property:Configuration Properties
alignment
spacing
insets
contentInsets
backgroundProperties
imageProperties
titleProperties
playsHaptics
tapToDismiss
appliesAdditionalInsetsAutomatically
animator
DefaultAnimator
, or to supply a customFlashAnimator
type.Animation
You can customize flash view animations using the
DefaultAnimator.Configuration
struct:For finer control over animations, create a custom type that conforms to
FlashAnimator
.Configuration Properties
duration
dampingRatio
initialVelocity
translateAmount
scaleCoefficient
1
will not scale the view.SwiftUI
Flash.swift does not yet expose a native SwiftUI interface, but you can show a flash message as a side effect:
License
Flash.swift is available under the MIT license. See the LICENSE file for more info.