Navigation Bar
Programmatic Title Setting
iOS Development
Mobile App UI
Code Tutorial

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.

Browse interview questions

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:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    self.title = "Home"
4}

In Objective-C:

objc
1- (void)viewDidLoad {
2    [super viewDidLoad];
3    self.title = @"Home";
4}

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:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    self.navigationItem.title = "Dashboard"
4}

In Objective-C:

objc
1- (void)viewDidLoad {
2    [super viewDidLoad];
3    self.navigationItem.title = @"Dashboard";
4}

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:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3
4    let titleLabel = UILabel()
5    titleLabel.text = "My App"
6    titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
7    titleLabel.textColor = .systemBlue
8    titleLabel.sizeToFit()
9
10    self.navigationItem.titleView = titleLabel
11}

In Objective-C:

objc
1- (void)viewDidLoad {
2    [super viewDidLoad];
3
4    UILabel *titleLabel = [[UILabel alloc] init];
5    titleLabel.text = @"My App";
6    titleLabel.font = [UIFont boldSystemFontOfSize:20];
7    titleLabel.textColor = [UIColor systemBlueColor];
8    [titleLabel sizeToFit];
9
10    self.navigationItem.titleView = titleLabel;
11}

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:

swift
func didReceiveData(itemCount: Int) {
    self.navigationItem.title = "Results (\(itemCount))"
}

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 for self.title but navigationItem may not behave as expected if accessed too early. Prefer viewDidLoad or 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.title when 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 titleView is 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 = true changes how titles render. Your custom titleView will not display in large-title mode, so plan your approach accordingly.

Summary

  • Use self.title for the simplest case where the navigation bar and tab bar should share the same label.
  • Use navigationItem.title when you need the navigation bar title to differ from the tab bar item title.
  • Use navigationItem.titleView when 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 UINavigationItem is a UIKit class available in both languages.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.