viewWillDisappear Determine whether view controller is being popped or is showing a sub-view controller
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
viewWillDisappear is called in several different navigation scenarios, so by itself it does not tell you why the screen is going away. The useful distinction is whether the controller is actually being removed from its parent, being dismissed modally, or simply being covered because another controller was pushed or presented.
Start With UIKit's Built-In Flags
Inside viewWillDisappear, the two most important checks are:
- '
isMovingFromParent' - '
isBeingDismissed'
Example:
This covers the most common cases.
What Happens on a Push
If a navigation controller pushes another view controller on top of the current one, the current controller's viewWillDisappear runs, but the current controller is not being popped. It is still in the navigation stack underneath the new screen.
In that case:
- '
isMovingFromParentisfalse' - '
isBeingDismissedis usuallyfalse'
That is why the final else branch above is useful. It usually means "I am disappearing, but I still exist."
What Happens on a Pop
If the back button is tapped or the controller is popped programmatically:
then viewWillDisappear still runs, but now isMovingFromParent becomes true. That is the clean signal that this controller is leaving the parent navigation stack.
This is often the right place to detect "user is leaving this screen permanently" for cleanup that should happen only on pop.
Modal Cases
If the controller was presented modally and is being dismissed:
then isBeingDismissed becomes true.
If the controller is inside a navigation controller that itself is dismissed modally, you may also need to consider the container. In more complex hierarchies, the disappearance reason can come from the parent being dismissed rather than the child acting alone.
A Practical Pattern
Here is a fuller version that distinguishes the main navigation outcomes:
The topViewController check is not always required, but it can help clarify push behavior while debugging a navigation flow.
Common Pitfalls
The most common mistake is assuming viewWillDisappear means the controller is being popped. It does not. Pushing another controller also triggers it.
Another issue is ignoring modal dismissal. If the controller can be presented modally, isMovingFromParent alone is not enough.
A third pitfall is doing destructive cleanup in every viewWillDisappear call. If the user is only moving to a child screen and may come back immediately, that cleanup can be premature.
Finally, complex container setups such as tab bars, split views, or embedded navigation controllers can make transitions less obvious. In those cases, inspect both the controller and its parent container state.
Summary
- '
viewWillDisappeardoes not by itself explain why a controller is disappearing.' - Use
isMovingFromParentto detect a pop from the navigation stack. - Use
isBeingDismissedto detect modal dismissal. - If both are false, the controller is often just being covered by another screen.
- Keep cleanup logic aligned with the actual navigation reason, not just the lifecycle callback.

