Fix: Detect Push vs Pop in viewWillDisappear in UIKit

Written by

in

Quick answer: In viewWillDisappear, check self.isMovingFromParent. If true, the view controller is being popped; if false, a new view controller was pushed onto the navigation stack. This property is updated before viewWillDisappear is called, making it the cleanest way to differentiate the two cases.

Why this happens

When a view controller disappears in a navigation stack, viewWillDisappear(_:) fires for two distinct reasons: a new view controller was pushed on top, or the current view controller was popped off the stack. The method itself doesn’t tell you which occurred. Historically developers inspected the navigation controller’s viewControllers array or set custom flags, but those approaches are fragile and add boilerplate.

The key insight is that UIKit updates the view controller hierarchy before calling viewWillDisappear(_:). By the time your implementation runs, isMovingFromParent (Swift) or isMovingFromParentViewController (Objective‑C) already reflects whether the view controller is being removed from its parent — i.e., popped. If the view controller is still in the parent’s children, a push happened instead.

This behavior is stable across iOS versions and works for both programmatic and interactive (swipe-back) pops. It also works when the view controller is presented modally and then dismissed, though the most common use case is navigation controller stacks.

How to fix it

1. Use isMovingFromParent (recommended)

This is the modern, concise API introduced in iOS 5. It returns true only when the view controller is being removed from its parent view controller — exactly what a pop does.

import UIKit

class DetailViewController: UIViewController {
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if isMovingFromParent {
            print("View controller was popped")
            // Clean up pop-specific state, save draft, etc.
        } else {
            print("New view controller was pushed")
            // Pause timers, stop location updates, etc.
        }
    }
}

Why this works: isMovingFromParent is a read‑only property on UIViewController. UIKit sets it to true during the pop transition before viewWillDisappear(_:) is dispatched. When a push occurs, the current view controller remains a child of the navigation controller, so the property stays false.

2. Legacy Objective‑C equivalent

If you maintain Objective‑C code, use isMovingFromParentViewController:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (self.isMovingFromParentViewController) {
        NSLog(@"View controller was popped");
    } else {
        NSLog(@"New view controller was pushed");
    }
}

3. Alternative: inspect the navigation stack (fallback)

Only use this if you cannot rely on isMovingFromParent (e.g., very old iOS versions or custom container view controllers). It checks the navigation controller’s viewControllers array after the transition has been prepared.

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    guard let nav = navigationController else { return }
    let vcs = nav.viewControllers

    if vcs.count >= 2, vcs[vcs.count - 2] == self {
        // Self is the second-to-last → a push occurred
        print("New view controller was pushed")
    } else if !vcs.contains(self) {
        // Self no longer in stack → popped
        print("View controller was popped")
    }
}

Caveats: This approach assumes a standard UINavigationController. Custom containers may not update viewControllers at the same time. It also breaks if the view controller is embedded in a tab bar or split view controller without a navigation controller.

How to prevent it

  • Always call super.viewWillDisappear(_:) first (or last in Objective‑C) to keep UIKit’s internal state consistent.
  • Prefer isMovingFromParent over manual flags; it eliminates a whole class of bugs where flags get out of sync.
  • If you need to know why a pop happened (back button, swipe gesture, popViewController(animated:)), the property doesn’t distinguish — treat all pops the same unless you have a specific requirement.
  • For modal presentations, isMovingFromParent also returns true when the presented view controller is dismissed, so the same check works there.

FAQ

Does isMovingFromParent work with interactive swipe-back gestures?

Yes. The property is set to true before viewWillDisappear(_:) fires regardless of whether the pop was triggered by the back button, a swipe gesture, or code.

Can I use this in a view controller that isn’t in a navigation controller?

Yes. isMovingFromParent reflects removal from any parent view controller (tab bar, split view, custom container). If the view controller has no parent, the property is always false.

What about viewDidDisappear — can I check there instead?

You can, but viewWillDisappear(_:) runs earlier, which is usually preferable for cancelling network requests, stopping animations, or saving transient state before the view is fully off‑screen. The property value is the same in both callbacks.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.