How to create a toast message in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIKit does not provide a built-in toast API the way Android does, so in Swift you usually create one yourself. A toast is just a lightweight temporary view with text, styling, and a fade animation. The cleanest implementation is a reusable helper that adds a label-backed container view, animates it in, waits briefly, and removes it.
A Simple Reusable Toast Helper
The following UIViewController extension creates a bottom-positioned toast with rounded corners and fade animations.
Use it from any view controller:
This is enough for many apps and avoids a third-party dependency.
Why This Approach Works Well
A toast is transient UI, so it should be:
- easy to call from anywhere
- visually lightweight
- non-blocking
- automatically dismissing
An extension on UIViewController keeps the API small while still giving the toast access to the visible view hierarchy and safe-area layout guides.
Using Auto Layout instead of hardcoded frames also makes the toast more robust across screen sizes and dynamic type changes.
Customize Position and Style
You can adapt the same helper to place the toast at the top, in the center, or above a toolbar. You can also add icons, blur effects, or haptic feedback if that matches the rest of the app design.
For example, to place it near the top instead of the bottom, constrain toastView.topAnchor to the safe area rather than bottomAnchor.
You can also make success and error styles distinct:
The toast should still remain subtle. If the message needs buttons or user acknowledgment, it is probably no longer a toast.
UIKit Versus SwiftUI
The code above is for UIKit. If your app is pure SwiftUI, you would usually build a toast as an overlay controlled by @State rather than by adding subviews directly.
That distinction matters because many "Swift toast" examples mix UIKit and SwiftUI assumptions. Pick the implementation model that matches the UI framework you are actually using.
Common Pitfalls
The most common mistake is using a hardcoded frame for the toast. That tends to break on rotation, different screen sizes, or devices with a home indicator.
Another issue is forgetting to remove the toast view after the animation. That leaves invisible views in the hierarchy.
Developers also sometimes use a toast for important errors or required confirmations. A toast is good for lightweight feedback, not for critical decisions.
Finally, if multiple toasts can appear at once, define a policy. Either queue them or replace the current toast, but do not let them stack randomly.
Summary
- UIKit has no built-in toast API, so a small reusable helper is the normal solution.
- Use a temporary styled container view plus fade-in and fade-out animations.
- Prefer Auto Layout and safe-area anchors over hardcoded frames.
- Keep toasts lightweight and non-blocking.
- Use a different UI pattern when the message needs interaction or acknowledgment.
Related reading
- How to create a UIImage with solid color?
- How to create a UIView bounce animation?
- How to create an array with incremented values in Swift?
- How to create an AVD for Android 4.0
- How to create an Empty Application in Xcode without Storyboard
- How to create an empty array in Swift?
- How to create an empty array in Swift?
- How to create an IBInspectable of type enum
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.