Swift
iOS Development
Toast Message
User Interface
Mobile App

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.

Browse interview questions

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.

swift
1import UIKit
2
3extension UIViewController {
4    func showToast(message: String, duration: TimeInterval = 2.0) {
5        let toastLabel = UILabel()
6        toastLabel.text = message
7        toastLabel.textColor = .white
8        toastLabel.font = .systemFont(ofSize: 14)
9        toastLabel.numberOfLines = 0
10        toastLabel.textAlignment = .center
11
12        let toastView = UIView()
13        toastView.backgroundColor = UIColor.black.withAlphaComponent(0.8)
14        toastView.layer.cornerRadius = 12
15        toastView.alpha = 0
16
17        toastView.addSubview(toastLabel)
18        view.addSubview(toastView)
19
20        toastLabel.translatesAutoresizingMaskIntoConstraints = false
21        toastView.translatesAutoresizingMaskIntoConstraints = false
22
23        NSLayoutConstraint.activate([
24            toastView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
25            toastView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -24),
26            toastView.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 24),
27            toastView.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -24),
28
29            toastLabel.topAnchor.constraint(equalTo: toastView.topAnchor, constant: 12),
30            toastLabel.bottomAnchor.constraint(equalTo: toastView.bottomAnchor, constant: -12),
31            toastLabel.leadingAnchor.constraint(equalTo: toastView.leadingAnchor, constant: 16),
32            toastLabel.trailingAnchor.constraint(equalTo: toastView.trailingAnchor, constant: -16),
33        ])
34
35        UIView.animate(withDuration: 0.25, animations: {
36            toastView.alpha = 1
37        }) { _ in
38            UIView.animate(withDuration: 0.25, delay: duration, options: [], animations: {
39                toastView.alpha = 0
40            }) { _ in
41                toastView.removeFromSuperview()
42            }
43        }
44    }
45}

Use it from any view controller:

swift
showToast(message: "Saved successfully")

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:

swift
1let isError = true
2toastView.backgroundColor = isError
3    ? UIColor.systemRed.withAlphaComponent(0.9)
4    : UIColor.black.withAlphaComponent(0.8)

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.