Removing viewcontrollers from navigation stack
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In UIKit, a UINavigationController stores a stack of view controllers that represent the user's navigation history. Removing controllers from that stack is possible, but you need to be explicit about whether you are popping the top controller, returning to a known controller, or replacing the stack entirely. The right approach depends on the user flow you want to preserve.
Use Pop Methods for Normal Back Navigation
If you only need to remove the current top controller, use popViewController. If you want to go all the way back to the root, use popToRootViewController.
These are the safest options because they follow normal navigation semantics. Use them whenever the user is simply moving backward.
Remove Specific Controllers by Replacing the Stack
If you want to remove one controller from the middle of the stack, you cannot do that with a single built-in "remove" call. Instead, read the current viewControllers array, filter it, and assign the new array back.
This is common after authentication or onboarding. For example, once the user has logged in successfully, you may want to remove the login screen so tapping Back does not return to it.
Because this replaces the stack directly, it should be used deliberately. You are editing the navigation history, not just moving through it.
Build the Desired Final Stack Explicitly
For complex flows, it is often cleaner to build the exact stack you want and set it once.
This is useful when you have finished a wizard, restored app state, or handled a deep link. Instead of pushing and popping several controllers, define the final state directly.
It also makes the intended navigation flow easier to understand during code review.
Be Careful With Lifecycle Assumptions
When you edit the viewControllers array directly, do not assume it behaves exactly like a series of animated pops. The navigation controller updates its stack, but controllers removed from the middle are not going through a standard back-navigation interaction.
That matters if your code relies on lifecycle callbacks to clean up resources or save state. If a screen needs explicit teardown logic, call that from a well-defined place in your coordinator or controller flow instead of depending only on navigation side effects.
A Coordinator Makes Stack Changes Safer
If your app has nontrivial flows, a coordinator or router object is usually a better place for stack manipulation than individual view controllers. That keeps navigation policy centralized.
This approach helps avoid scattered code where every screen edits the stack differently.
Common Pitfalls
- Using direct stack replacement when a simple
popViewControllerwould be clearer. - Removing screens from the middle of the stack without thinking through the resulting Back behavior.
- Editing the navigation stack inside multiple view controllers instead of centralizing the flow.
- Assuming direct stack mutation behaves exactly like animated pops for lifecycle handling.
- Forgetting that a removed login or onboarding controller cannot be reached again through normal back navigation.
Summary
- Use
popViewControllerandpopToRootViewControllerfor normal backward navigation. - To remove specific controllers, read
viewControllers, filter or rebuild it, and callsetViewControllers. - Direct stack replacement is appropriate for login, onboarding, restore-state, and deep-link flows.
- Treat navigation-stack edits as flow-control decisions, not small UI tweaks.
- Centralizing navigation logic in a coordinator makes these operations easier to maintain.
Related reading
- Rename a dictionary key
- Reorder vector using a vector of indices
- Reordering a list to maximize difference of adjacent elements
- Reordering of array elements
- Renew Provisioning Profile
- Renew Push certificate and keep current App Store App working
- Repeatedly removing the maximum average subarray
- Replace a character at a specific index in a string?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.