Warning -Presenting view controllers on detached view controllers is discouraged
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The warning "Presenting view controllers on detached view controllers is discouraged" appears when you call present(_:animated:completion:) on a view controller that is not part of the active view controller hierarchy. A view controller is "detached" when it has been removed from its parent, dismissed, or not yet added to the window's root hierarchy. The fix is to ensure you always present from the topmost visible view controller — typically the root view controller or the currently presented controller.
What Causes the Warning
Calling present() in viewDidLoad() is risky because the view controller may not yet be added to the window hierarchy. The view is loaded but the controller may not be "attached" to the display chain.
Fix 1: Present in viewDidAppear
viewDidAppear is called after the view controller is visible and part of the active hierarchy. This is the safest lifecycle method for presenting other view controllers.
Fix 2: Present from the Top View Controller
This utility finds the topmost visible view controller by traversing the presentation chain from the root. It handles navigation controllers, tab controllers, and modally presented controllers.
Fix 3: Present from the Root View Controller
Presenting from the root view controller works when you do not have a deep presentation chain. If another controller is already presented, dismiss it first.
Common Scenario: Presenting After Dismiss
Always present the next view controller in the dismiss completion handler. Presenting before the dismissal animation completes causes the detached warning because the presenting controller is still mid-transition.
Common Scenario: Presenting from a Child View Controller
Child view controllers added via addChild() are in the hierarchy but may not be the right presenter. Use parent to delegate presentation upward.
Debugging the Issue
Check viewIfLoaded?.window != nil to verify a view controller is attached to a window. If it is nil, the controller is detached and should not present other controllers.
Common Pitfalls
- Presenting in viewDidLoad or viewWillAppear: The view controller may not be fully in the hierarchy during these lifecycle methods. Wait until
viewDidAppearto present other controllers. - Not using the dismiss completion handler: Calling
present()immediately afterdismiss()without waiting for the completion block causes the warning. Always present in thecompletionclosure ofdismiss. - Presenting from a view controller that was popped from a navigation stack: After
popViewController, the popped controller is detached. Any delayed callbacks (network responses, timers) that try to present from it trigger the warning. - Multiple scenes on iPad: On iPad with multiple scenes,
UIApplication.shared.keyWindowis deprecated and may return the wrong window. UseUIWindowSceneto find the correct window for the active scene. - Ignoring the warning: While the presentation may still appear to work, detached presentations can cause visual glitches, broken transitions, and crashes on certain iOS versions. Always fix the warning.
Summary
- The warning means you are presenting from a view controller not in the active hierarchy
- Present in
viewDidAppear, notviewDidLoadorviewWillAppear - Use a
topMostview controller utility to find the correct presenter - Always present the next controller in the
dismisscompletion handler - Check
viewIfLoaded?.window != nilto verify a controller is attached - Delegate presentation upward using
parentwhen presenting from child view controllers
Related reading
- WARNING API 'variant.getJavaCompile' is obsolete and has been replaced with 'variant.getJavaCompileProvider
- Warning Attempt to present on whose view is not in the window hierarchy - swift
- warning 'characters' is deprecated Please use String or Substring directly
- Warning frame for Navigation bar will be different at run time appears in Xcode 8 Swift 3
- Warning frame for Navigation bar will be different at run time appears in Xcode 8 Swift 3
- Warning Kotlin plugin version is not the same as library version but it is
- Warning The Copy Bundle Resources build phase contains this target's Info.plist file
- Warning This AsyncTask class should be static or leaks might occur
.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.