How to use presentModalViewController to create a transparent view
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To present a transparent modal view controller in iOS, set the presented view controller's modalPresentationStyle to .overFullScreen (or .overCurrentContext) and give its view a semi-transparent background color. The older presentModalViewController:animated: method was deprecated in iOS 6 — use present(_:animated:completion:) instead. The key is .overFullScreen, which keeps the presenting view controller's view in the hierarchy so it is visible behind the transparent overlay.
Basic Transparent Modal (Swift)
The .overFullScreen style tells UIKit to keep the presenting view controller's view in the hierarchy. Without this, UIKit removes it after the transition, and the background turns black.
Why .fullScreen Does Not Work
With .fullScreen, UIKit removes the presenting view controller's view from the window hierarchy after the presentation animation completes. Even though the overlay's background is semi-transparent, there is nothing behind it to show — just the window's background color.
Using .overCurrentContext
.overCurrentContext is similar to .overFullScreen but respects the presenting view controller's definesPresentationContext property. In a tab bar or navigation controller setup, it overlays only the current content area, not the full screen.
Custom Transition Animation
Objective-C Version
SwiftUI Equivalent
Common Pitfalls
- Using
.fullScreeninstead of.overFullScreen:.fullScreenremoves the presenting view from the window hierarchy after the transition. Even with a transparent background, you see black (the window color). Always use.overFullScreenor.overCurrentContextfor transparent overlays. - Setting background color before presentation instead of in
viewDidLoad: If you setoverlayVC.view.backgroundColorbefore presenting, the view may not be loaded yet and the setting is lost. Set the background color in the overlay'sviewDidLoadmethod, which runs after the view is created. - Forgetting
definesPresentationContextwith.overCurrentContext: When presenting from a child of a container (tab bar, navigation),.overCurrentContextlooks for the nearest ancestor withdefinesPresentationContext = true. If none is set, the presentation falls back to the root view controller. SetdefinesPresentationContext = trueon the appropriate parent to control which content is overlaid. - Using the deprecated
presentModalViewController:animated:: This method was deprecated in iOS 6. It does not support modern presentation styles like.overFullScreen. Usepresent(_:animated:completion:)to access the full range ofUIModalPresentationStyleoptions. - SwiftUI
fullScreenCovernot supporting transparent backgrounds natively: SwiftUI'sfullScreenCoveruses.fullScreenpresentation by default, which removes the background. Workarounds require a UIViewRepresentable to clear the hosting view's background, or using a ZStack overlay instead of a modal presentation.
Summary
- Use
.overFullScreenas themodalPresentationStyleto keep the presenting view visible behind the overlay - Set the overlay's background to a semi-transparent color in
viewDidLoad - Use
.crossDissolveas themodalTransitionStylefor a smooth fade-in effect - For SwiftUI, use a ZStack overlay or a
ClearBackgroundViewUIViewRepresentable workaround - The deprecated
presentModalViewController:animated:does not support modern presentation styles — usepresent(_:animated:completion:)instead
Related reading
- How to use pull-to-refresh in Swift?
- How to use pull-to-refresh in Swift?
- How to use putExtra and getExtra for string data
- How to use RecyclerView inside NestedScrollView?
- How to use Runnable in Monodroid for Android
- How to use SCNetworkReachability in Swift
- How to use ScrollView in Android?
- How to use SharedPreferences in Android to store, fetch and edit values
.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.