Swift
iOS
ModalView
Transparency
Duplicate

Swift Modal View Controller with transparent background

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

To present a modal view controller with a transparent or dimmed background in iOS, the critical choice is the presentation style. A normal full-screen modal replaces the underlying interface, so transparency only works when the modal is presented “over” the current content instead of replacing it completely.

Use an Over-Current Presentation Style

The usual pattern is to set the modal presentation style to .overCurrentContext or .overFullScreen and make the presented view’s background color transparent:

swift
1import UIKit
2
3final class PopupViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
8    }
9}
10
11final class HostViewController: UIViewController {
12    func showPopup() {
13        let popup = PopupViewController()
14        popup.modalPresentationStyle = .overCurrentContext
15        popup.modalTransitionStyle = .crossDissolve
16        definesPresentationContext = true
17        present(popup, animated: true)
18    }
19}

This preserves the underlying screen visually and lets the modal draw a translucent overlay on top of it.

overCurrentContext Versus overFullScreen

Both styles can work, but they behave slightly differently:

  • '.overCurrentContext presents over the current presentation context'
  • '.overFullScreen presents over the full screen while still allowing transparency'

If the presenting controller is embedded in a navigation controller or another container, definesPresentationContext = true often matters so iOS knows which view hierarchy should host the overlay.

Build the Transparent Effect Intentionally

A transparent modal usually has two layers:

  • a dimmed or clear background
  • a foreground content card

For example:

swift
1import UIKit
2
3final class PopupViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
8
9        let card = UIView()
10        card.translatesAutoresizingMaskIntoConstraints = false
11        card.backgroundColor = .white
12        card.layer.cornerRadius = 12
13
14        view.addSubview(card)
15
16        NSLayoutConstraint.activate([
17            card.centerXAnchor.constraint(equalTo: view.centerXAnchor),
18            card.centerYAnchor.constraint(equalTo: view.centerYAnchor),
19            card.widthAnchor.constraint(equalToConstant: 240),
20            card.heightAnchor.constraint(equalToConstant: 160)
21        ])
22    }
23}

The modal view itself stays transparent or translucent, while the card remains fully opaque and readable.

Dismissal and Touch Handling

If you want taps outside the card to dismiss the modal, add a gesture recognizer to the background view:

swift
1let tap = UITapGestureRecognizer(target: self, action: #selector(close))
2view.addGestureRecognizer(tap)
3
4@objc private func close() {
5    dismiss(animated: true)
6}

In real UI code, you often want the tap recognizer to ignore touches inside the content card. That can be done with gesture recognizer delegation or by placing the gesture on a separate background view instead of the root content view.

Storyboard Version

If the modal comes from a storyboard, the same idea applies:

  • set the presentation style to Over Current Context or Over Full Screen
  • make the background clear or semi-transparent
  • ensure the presenting controller defines the presentation context if needed

The mechanics are the same whether the controller is created in code or in Interface Builder.

Common Pitfalls

The most common mistake is leaving the modal presentation style at the default full-screen mode. In that case, the presenting view is removed from the visual stack, so “transparent background” cannot reveal anything underneath.

Another pitfall is forgetting definesPresentationContext when using .overCurrentContext inside container hierarchies. Without the right presentation context, the overlay may not appear where you expect.

It is also easy to set the modal’s root view background to .clear and then wonder why nothing is visible. A good transparent modal usually needs a deliberate dim view plus an opaque content view on top.

Finally, be careful with tap handling. If the entire root view dismisses on tap, touches intended for buttons inside the popup can become awkward unless the gesture is scoped correctly.

Summary

  • Use .overCurrentContext or .overFullScreen for transparent modal presentation.
  • Set the presented view’s background to a transparent or semi-transparent color.
  • Add an opaque content card on top of the translucent background.
  • Use definesPresentationContext when the presentation should stay within the current controller hierarchy.
  • Default full-screen presentation will not preserve the underlying view for transparency.

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.