presentViewController and displaying navigation bar
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Calling present on a view controller does not automatically give the presented screen a navigation bar. A navigation bar appears only when the presented controller is inside a UINavigationController, or when you push onto an existing navigation stack instead of presenting modally.
present and push Solve Different Problems
In UIKit, modal presentation and navigation-stack pushing are separate concepts.
- '
present(_:animated:)shows a new controller modally.' - '
navigationController?.pushViewController(_:animated:)pushes onto an existing navigation stack.'
If you present a plain UIViewController, there is no navigation controller wrapped around it by default, so there is no navigation bar to display.
The Common Fix: Present a Navigation Controller
If you want a modally presented screen that still has a navigation bar, embed the destination controller in a UINavigationController first.
Now the modal screen gets a navigation bar because the visible controller lives inside a navigation controller.
If You Are Already in a Navigation Stack, Push Instead
Sometimes the real issue is that the screen should not be modal in the first place. If the current controller already belongs to a navigation controller and the user expects back navigation, pushing is often the better UX.
This automatically shows the navigation bar from the existing stack, along with the standard back button behavior.
Storyboard Version
If you use storyboards, the same principle applies. Either:
- embed the destination scene in a navigation controller before presenting it,
- or segue into the current navigation stack with a push segue instead of a modal segue.
The problem is not storyboard-specific. It is about whether a navigation controller exists in the presentation chain.
Configuring the Navigation Bar
Once the presented controller is embedded properly, configure it through navigationItem.
You can also customize bar buttons, title behavior, and appearance the same way you would for any pushed controller.
Full-Screen and Sheet Presentations
Modern iOS often presents controllers as sheets. That does not change the core rule: sheet or full-screen, a navigation bar still requires a navigation controller if you want top-bar navigation controls.
This gives you a sheet presentation with a proper navigation bar.
Why navigationController Is Sometimes nil
Developers often try to access self.navigationController in a presented controller and find it is nil. That happens when the controller was presented directly and not embedded in a navigation controller.
Once you present UINavigationController(rootViewController: someVC), the child controller's navigationController property is no longer nil.
Common Pitfalls
The biggest pitfall is assuming modal presentation automatically includes navigation UI. It does not.
Another mistake is using present when the user flow really wants hierarchical navigation. In that case, pushViewController is more appropriate and simpler.
Developers also sometimes embed the wrong controller. The navigation controller must wrap the destination controller before presentation, not after it is already on screen.
Finally, if the navigation bar still does not appear, check whether you accidentally hid it elsewhere with setNavigationBarHidden.
Summary
- '
presentdoes not create a navigation bar on its own.' - To show a navigation bar modally, present a
UINavigationControllerwhose root is your destination controller. - If the screen belongs in the current flow, use
pushViewControllerinstead ofpresent. - Configure titles and buttons through
navigationItemafter embedding correctly. - If
navigationControllerisnil, the controller was probably presented without a navigation wrapper.
Related reading
- presentViewController crash on iOS 6 AutoLayout
- presentViewControlleranimatedYES view will not appear until user taps again
- Prevent Android activity dialog from closing on outside touch
- Prevent Android activity dialog from closing on outside touch
- Prevent dialog from closing on outside touch in Flutter
- Prevent screen capture in an iOS app
- Prevent screen rotation on Android
- Prevent segue in prepareForSegue method?
.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.