Is it possible to determine whether ViewController is presented as Modal?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
There is no single isModal property on UIViewController in UIKit. However, you can determine if a view controller was presented modally by checking several properties: presentingViewController is non-nil when presented modally, isBeingPresented is true during the presentation transition, and the view controller is not part of a navigation stack push. The most reliable approach combines multiple checks because navigation controllers can themselves be presented modally.
Basic Check: presentingViewController
This checks three scenarios: direct modal presentation, modal navigation controller, and modal tab bar controller.
Checking During Presentation Lifecycle
isBeingPresented is true only during the view controller's initial modal presentation transition. isMovingToParent is true when being pushed onto a navigation controller.
More Robust Check
Determining Presentation in SwiftUI
In SwiftUI, @Environment(\.isPresented) indicates whether the view is inside a modal presentation context.
Adding a Close Button Conditionally
Checking modalPresentationStyle
Common Pitfalls
- Checking
presentingViewControllertoo early: InviewDidLoad,presentingViewControllermay not yet be set. Check inviewWillAppearorviewDidAppearfor reliable results. - Forgetting that navigation controllers can be modally presented: When a
UINavigationControlleris presented modally and your view controller is its root,self.presentingViewControlleris nil — the navigation controller is the presented one. ChecknavigationController?.presentingViewControlleras well. - Assuming
isBeingPresentedpersists after presentation:isBeingPresentedis onlytrueduring the presentation transition (inviewWillAppear/viewDidAppear). Checking it later always returnsfalse. Store the value in a property if needed after the transition completes. - Relying on
modalPresentationStylebefore presentation: ThemodalPresentationStyleproperty reflects what was set on the view controller, not necessarily the actual presentation style used. The system may adapt the style (e.g., on iPad). CheckpresentationController?.adaptivePresentationStylefor the actual style. - Not handling iOS 13+ card-style modals: Since iOS 13, the default modal presentation is
.pageSheet(a card), not.fullScreen. Users can swipe down to dismiss without calling your dismiss logic. ImplementUIAdaptivePresentationControllerDelegateto handle interactive dismissal.
Summary
- Check
presentingViewController != nilas the primary indicator of modal presentation - Also check
navigationController?.presentingViewControllerfor modally-presented navigation controllers - Use
isBeingPresentedinviewWillAppear/viewDidAppearfor transition-time checks only - Conditionally show a close/dismiss button based on the
isModalcheck - In SwiftUI, use
@Environment(\.isPresented)to determine modal context - Handle iOS 13+ card-style dismissal by implementing
UIAdaptivePresentationControllerDelegate

