UINavigationBar - Set title programmatically?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Setting a navigation title programmatically in iOS is straightforward, but teams often hit issues when using large titles, custom title views, UINavigationController hierarchies, or SwiftUI bridges. The correct API depends on where you want the title state to live.
In UIKit, the title belongs to UINavigationItem of the current view controller. In practice, you usually set self.title or navigationItem.title inside viewDidLoad/viewWillAppear.
Core Sections
1. Basic title setting in UIKit
This works when the controller is embedded in a UINavigationController.
If title depends on dynamic data loaded later:
2. Large titles and appearance configuration
Large titles require both nav controller preference and item display mode.
Global appearance example:
3. Custom title views
For logos or composite title layouts, use navigationItem.titleView.
You can set an image view similarly. Keep title view compact and accessible.
4. Timing and lifecycle considerations
If title changes based on state that can update while returning to screen, set in viewWillAppear.
In tab-based apps with nested navigation controllers, ensure you are updating the visible controller’s navigationItem, not a parent container.
Common Pitfalls
- Setting title on a controller that is not embedded in a
UINavigationController. - Updating
navigationController?.titleinstead of current view controller’snavigationItem.title. - Expecting large titles without enabling
prefersLargeTitlesand display mode. - Overusing custom
titleViewwith complex layout that breaks in compact widths. - Changing title in background callbacks without dispatching UI updates to main thread.
Summary
Programmatic navigation titles in UIKit should be set on the current controller’s navigationItem (or title). Add large-title or custom-view configuration as needed, and choose lifecycle timing based on whether title is static or dynamic. Keeping title ownership clear prevents most navigation bar inconsistencies.
In apps with localization, avoid hardcoded titles in controllers wherever possible. Bind title strings through localization helpers or view models so language changes and A/B text updates remain centralized. This also helps testing because title logic can be validated independently from view lifecycle quirks. If title depends on asynchronous data (for example fetched entity name), update it on the main thread and provide a sensible placeholder title to avoid blank navigation bars during loading.
When mixing UIKit and SwiftUI, title ownership can become confusing. If a SwiftUI view is hosted inside a navigation controller, make sure only one layer is responsible for setting title at a time. Conflicting updates from both frameworks can cause flicker or unexpected overrides. Establishing a clear convention per screen keeps navigation behavior predictable.
In complex apps, adding navigation-title assertions to UI tests can prevent regressions where titles disappear after refactors in coordinator or routing layers. This is especially useful for flows that reuse controllers and update title dynamically as state changes.
Another practical tip is to keep title update logic in one method per controller instead of scattering assignments across callbacks. Centralized updates make future changes to localization, analytics, and accessibility labels easier to maintain.
Consistency in title ownership greatly reduces navigation bugs.
Related reading
- UINavigationBar custom back button without title
- UINavigationBar Hide back Button Text
- UINavigationController back button custom text?
- UINavigationController without navigation bar?
- UIPageViewController return the current visible view
- UIPanGestureRecognizer - Only vertical or horizontal
- UIPopovercontroller dealloc reached while popover is still visible
- UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController
.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.