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.
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:
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:
- '
.overCurrentContextpresents over the current presentation context' - '
.overFullScreenpresents 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:
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:
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
.overCurrentContextor.overFullScreenfor 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
definesPresentationContextwhen the presentation should stay within the current controller hierarchy. - Default full-screen presentation will not preserve the underlying view for transparency.
Related reading
- Swift Open Link in Safari
- Swift optional escaping closure parameter
- Swift Pass array by reference?
- Swift performSegueWithIdentifier not working
- Swift performSelectorwithObjectafterDelay is unavailable
- Swift performSelectorwithObjectafterDelay is unavailable
- Swift pods cannot yet be integrated as static libraries FirebaseCoreInternal-library
- Swift print vs println vs NSLog
.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.