How to set the title of a Navigation Bar programmatically?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In iOS development, the navigation bar at the top of the screen serves as the primary way to communicate where the user is within your app's hierarchy. Setting its title programmatically is one of the most common tasks you will encounter when building UIKit-based applications. There are three distinct approaches to controlling what appears in the navigation bar title area, and understanding when to use each one will save you from subtle bugs and unexpected behavior.
Setting the Title with self.title
The simplest way to set a navigation bar title is through the view controller's title property. When a view controller is embedded inside a UINavigationController, the navigation bar automatically reads from this property.
In Swift:
In Objective-C:
Setting self.title has a side effect you should know about: it also sets the title of the corresponding tab bar item if the view controller is inside a UITabBarController. This means both the navigation bar and the tab bar label will display the same string. If that is the behavior you want, self.title is the most convenient choice.
Setting the Title with navigationItem.title
If you need the navigation bar title to differ from the tab bar item title, use navigationItem.title instead. This property only affects the navigation bar and leaves the tab bar item untouched.
In Swift:
In Objective-C:
A practical example: imagine a tab labeled "Settings" in the tab bar, but the navigation bar should read "Account Settings" when the user is on a particular screen. Setting navigationItem.title to "Account Settings" achieves this without overwriting the tab label.
The priority rule is straightforward: if navigationItem.title is set, the navigation bar uses it. If it is nil, the navigation bar falls back to self.title.
Using a Custom titleView
Sometimes a plain text title is not enough. You may need a logo, a segmented control, or a search bar in the title area. For these cases, assign a custom UIView to navigationItem.titleView.
In Swift:
In Objective-C:
You can replace the UILabel with any UIView subclass. A common pattern is to embed a UIImageView to display a brand logo, or a UISegmentedControl to let the user switch between content modes directly from the navigation bar.
When titleView is set, the navigation bar ignores both self.title and navigationItem.title for display purposes, though those string properties remain accessible in code.
Updating the Title Dynamically
Navigation bar titles often need to change at runtime, for example when the user switches between data sets or when a network request completes. You can update the title at any point in the view controller lifecycle:
There is no need to call any refresh or reload method. UIKit picks up the new value immediately.
Common Pitfalls
- Setting the title before the view controller is pushed onto the navigation stack. If you set the title in
init, it works forself.titlebutnavigationItemmay not behave as expected if accessed too early. PreferviewDidLoador later. - Forgetting that self.title propagates to the tab bar item. Developers often wonder why their tab label changed after setting the navigation title. Use
navigationItem.titlewhen you need them to differ. - Not calling sizeToFit on a custom titleView. If the custom view has no intrinsic content size and you skip layout, it may appear with zero width and be invisible.
- Assuming titleView and title string work together. When
titleViewis set, the string-based title is completely hidden. You cannot show both at the same time. - Ignoring large title mode in iOS 11 and later. Setting
navigationController?.navigationBar.prefersLargeTitles = truechanges how titles render. Your customtitleViewwill not display in large-title mode, so plan your approach accordingly.
Summary
- Use
self.titlefor the simplest case where the navigation bar and tab bar should share the same label. - Use
navigationItem.titlewhen you need the navigation bar title to differ from the tab bar item title. - Use
navigationItem.titleViewwhen you need a fully custom view such as a logo, segmented control, or styled label. - Titles can be updated at any time during the view controller lifecycle, and UIKit reflects the change immediately.
- Both Swift and Objective-C follow the same API patterns since
UINavigationItemis a UIKit class available in both languages.
Related reading
- How to set the title text color of UIButton?
- How to set the title text color of UIButton?
- How to set the width of a cell in a UITableView in grouped style
- How to set timeout in Retrofit library?
- How to set timeout in Retrofit library?
- How to set timer in android?
- How to set top-left alignment for UILabel for iOS application?
- How to set UICollectionViewCell Width and Height programmatically
.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.