Programmatically call navigation controller back button on iOS
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On iOS, you normally do not “press the back button” programmatically. Instead, you perform the same navigation action that the back button would perform, which is usually popping the current view controller from a UINavigationController stack. The exact method depends on how the screen was shown: pushed on a navigation stack or presented modally.
Use popViewController for Standard Back Navigation
If the current screen was pushed with navigationController?.pushViewController(...), the programmatic equivalent of tapping Back is:
That removes the current view controller from the navigation stack and reveals the previous one.
A simple example:
This is the standard answer for most cases.
Use dismiss for Modal Screens
If the screen was shown with present(...) rather than pushed onto a navigation controller, Back is not the right concept. In that case, dismiss the presented screen.
A common mistake is calling popViewController on a screen that is actually modal. That does nothing if there is no navigation stack to pop from.
Use popToRootViewController or popToViewController for Custom Flow
Sometimes you do not want to go back by only one screen. UINavigationController gives you other stack operations.
Or to pop back to a specific screen:
These are navigation-stack operations, not simulated button taps.
Do Not Call the Back Button's Action Directly
Developers sometimes try to trigger the back button by reaching into navigationItem.leftBarButtonItem and calling its selector. That is the wrong level of abstraction. The bar button is only UI for a navigation action. The real behavior belongs to the navigation controller.
If you need custom cleanup before leaving the screen, do that work and then call the correct navigation method.
Handle Unsaved Changes Explicitly
Programmatic back navigation is often used after form submission or cancellation. If the screen contains unsaved changes, confirm with the user before popping or dismissing.
That keeps the navigation behavior predictable and safe.
Custom Back Buttons Should Still Use the Navigation API
If you replace the default back button with your own bar button item, the handler should still call popViewController or dismiss, not duplicate stack logic manually. That preserves the navigation controller's normal behavior and keeps the code easier to reason about.
It also keeps gesture-based back navigation and stack semantics aligned with the rest of the app.
Common Pitfalls
- Calling
popViewControllerwhen the screen was presented modally. - Trying to invoke the back button UI instead of performing the navigation action.
- Forgetting that
navigationControllercan benilif the view controller is not embedded in one. - Using back navigation to hide underlying state-management problems.
- Popping multiple screens accidentally when you only wanted one level back.
Summary
- The usual programmatic equivalent of the Back button is
navigationController?.popViewController(animated: true). - Use
dismiss(animated:)for modally presented screens. - Use
popToRootViewControllerorpopToViewControllerfor custom back-navigation flows. - Do not try to simulate a tap on the bar button item.
- Choose the navigation method that matches how the screen was presented.
Related reading
- Programmatically change input type of the EditText from PASSWORD to NORMAL vice versa
- Programmatically change UITextField Keyboard type
- Programmatically create a UIView with color gradient
- Programmatically get height of navigation bar
- Programmatically get own phone number in iOS
- Programmatically go back to previous ViewController in Swift
- Programmatically go back to previous ViewController in Swift
- Programmatically go back to the previous fragment in the backstack
.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.