Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Attempting to present a UIViewController on another UIViewController whose view is not in the window hierarchy is a common mistake that iOS developers encounter. This article delves into technical explanations, examples, and solutions to better understand and solve this issue.
Understanding the Hierarchy
Every UIViewController in iOS has an associated view that is part of a view hierarchy. When presenting a new UIViewController, the presenting view controller's view must be in the current window's hierarchy to ensure a seamless and functional user interface. Presenting a view controller involves placing it onto the view hierarchy, making the new view controller the focus of user interaction.
The View Hierarchy
The view hierarchy is a tree-like structure composed of views that render content on the screen. At the top of this hierarchy is the window (UIWindow), which acts as a backdrop for all UIViewControllers. A typical structure can be depicted as follows:
When attempting to present a view controller, iOS expects the presenting view controller's view to be visible within this structure.
Common Causes of the "Not in the Window Hierarchy" Error
- View Controller is Not Visible: Attempting to present a view controller when the current one isn't displayed can result in this error.
- Incorrect View Transitions: Mismanaging view transitions or failing to add the presenting view controller's view to the window hierarchy can lead to issues.
- Asynchronous Operations: Starting asynchronous tasks that attempt to present view controllers upon completion without checking the view hierarchy state is a common pitfall.
Diagnosing the Problem
Symptom
This error often results in a runtime exception with a message similar to:
Debugging Steps
- Check Hierarchy Presence: Verify that the view controller attempting to present another is indeed part of the window hierarchy.
- Utilize Lifecycle Methods: Make sure presentations wait until the viewDidAppear phase of the lifecycle, as this guarantees the view is in the window hierarchy.
Solutions
Delayed Presentation
Oftentimes, ensuring the presentation code runs when the view is part of the hierarchy is enough. Using lifecycle methods is a standard approach:
Reattach Views
If you must present while the current view is detached from the hierarchy, consider reattaching:
Best Practices
- Always ensure that your view controller's view is a part of the window hierarchy before presenting new view controllers.
- Utilize Apple's lifecycle events to manage view controllers correctly.
- Use completion handlers from asynchronous processes to verify view hierarchy state before attempting changes.
Summary Table
| Issue/Technique | Description |
| Common Error | Attempt to present on a view controller whose view is not in the hierarchy |
| Presentation Failure | Occurs during a runtime if hierarchy checks are not in place |
| Lifecycle Use | Use viewDidAppear to safely present new view controllers |
| Reattach Strategy | Temporarily attach view controller views to the window if necessary |
| Best Practice | Ensure all view controllers use proper lifecycle awareness and asynchronous operation management |
In conclusion, understanding and managing the view hierarchy is crucial to ensure smooth transitions and interactions in your app. By adhering to lifecycle methods and verifying hierarchy presence, developers can avoid errors relating to view presentations.

