222

I have a navigation bar with a title. When I double click the text to rename it, it actually says it's a navigation item, so it might be that.

I'm trying to change the text using code, like:

declare navigation bar as navagationbar here
button stuff {
    navigationbar.text = "title"
}

That's not my code obviously, just showing how it would work.

So whenever I press the button, I want the title to change.

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Rising
  • 2,465
  • 3
  • 13
  • 8

18 Answers18

616

You change the title by changing the title of the view controller being displayed:

viewController.title = "some title"

Normally this is done in view did load on the view controller:

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "some title"
}

However, this only works if you have your view controller embedded in a UINavigationController. I highly recommend doing this instead of creating a navigation bar yourself. If you insist on creating a navigation bar yourself, you can change the title by doing:

navigationBar.topItem.title = "some title"
drewag
  • 93,393
  • 28
  • 139
  • 128
  • 1
    ViewController.type does not have a member named title. That's the error I get. – Rising Aug 06 '14 at 18:27
  • 3
    You don't call it directly on the class. You call it on an instance of a subclass of UIViewController – drewag Aug 06 '14 at 18:28
  • Yes, `ViewController` refers to the class itself. You need to refer to an **instance** of `ViewController`. From within a method on your class, such as `viewDidLoad`, you can do so using `self`. If you do not know the difference between a class and an instance, I highly recommend you learn what it is. [This article](http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/) will help. – drewag Aug 06 '14 at 18:34
  • Works like a charm in Xcode as well.. just a small change -> self.title = @"some title"; – Abstract Nov 04 '15 at 01:05
  • self.title = "some title" worked for me. Thank you ! I have been trying 'navigationController?.navigationItem.title = "Some title". really not sure why it doesn't work. (I do have a navigation item there,which I placed to display the title, in addition to the embedded navigation controller ). I still think it should work, but dont know why. Stack overflow to the rescue once again ! – Naishta Jan 08 '16 at 22:37
  • @Naishta navigationItem is a part of every UIViewController. A navigation controller shows the navigation item of the current view controller. You other code is setting the navigationItem on the navigation controller itself which would only apply if another navigation controller was trying to display your navigation controller. – drewag Jan 09 '16 at 21:14
  • Thanks. I was using self.navigationController?.navigationBar.topItem?.title = "Settings" but this works just as well and is simpler. – Nabha Cosley Mar 03 '16 at 06:06
  • @drewag what is the reasoning behind this statement please? : "I highly recommend doing this instead of creating a navigation bar yourself". – oyalhi May 23 '16 at 03:48
  • @oyalhi I have experienced several bugs in the past from not using a navigation controller. Most have been related to OS upgrades. The less custom view programming you can do, the more resilient your app will be to OS upgrades. For example, I saw lots of issues (I am a contract developer so i work on many projects) during the transition to the new style status bar. I had to do a lot of shifting of views down but a UINavigationController did this automatically because Apple did it for us. – drewag May 24 '16 at 04:04
  • 3
    Warning: `self.title = "some title"` will change the navigation bar title AND the tab bar item title. – RemyDCF Jan 18 '17 at 21:30
  • @drewag, could you help my problem please https://stackoverflow.com/questions/44304498/how-to-set-font-family-to-uiviewcontroller-title-in-swift ? – May Phyu Jun 02 '17 at 04:37
  • and use this to change title of navigation bar title in a tab bar : `tabBarController?.navigationItem.title = "title"` – MHSaffari Feb 17 '18 at 07:04
  • shouldn't this be in viewWillAppear()? – Varun Goyal May 11 '19 at 16:14
83

Try the following in viewDidLoad

self.navigationItem.title = "Your Title"
kbunarjo
  • 1,277
  • 2
  • 11
  • 27
Avi
  • 2,196
  • 18
  • 18
21

The code below works for me with Xcode 7:

override func viewDidLoad() {        
    super.viewDidLoad()
    self.navigationItem.title = "Your Title"
}
kbunarjo
  • 1,277
  • 2
  • 11
  • 27
grmonkeybiz
  • 211
  • 2
  • 2
13

I found this to work:

navigationItem.title = "Title"
AJ9
  • 1,256
  • 1
  • 17
  • 28
  • How do you center the title in a view controller which is a child of a UINavigationController? For some reason the text is aligned to the left. `var customTitle = self.navigationController?.visibleViewController?.navigationItem customTitle?.title = "Some Title"` – bibscy May 21 '17 at 10:21
10

Swift 5.1

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "What ever you want"
}
Marlhex
  • 1,870
  • 19
  • 27
7

If you have a NavigationController embedded inside of a TabBarController see below:

super.tabBarController?.title = "foobar"

enter image description here

You can debug issues like this with debugger scripts. Try Chisel's pvc command to print every visible / hidden view on the hierarchy.

rustyMagnet
  • 3,479
  • 1
  • 31
  • 41
6

and also if you will try to create Navigation Bar manually this code will help you

func setNavBarToTheView() {
    let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 64.0))
    self.view.addSubview(navBar);
    let navItem = UINavigationItem(title: "Camera");
    let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel, target: self, action: #selector(CameraViewController.onClickBack));
    navItem.leftBarButtonItem = doneItem;
    navBar.setItems([navItem], animated: true);
}
Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
6

The correct answer for people using would be

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Your Text"
}
milo526
  • 5,012
  • 5
  • 41
  • 60
Kevin Singh
  • 421
  • 8
  • 14
5

Normally, the best-practice is to set the title on the UIViewController. By doing this, the UINavigationItem is also set. Generally, this is better than programmatically allocating and initializing a UINavigationBar that's not linked to anything.

You miss out on some of the benefits and functionality that the UINavigationBar was designed for. Here is a link to the documentation that may help you. It discusses the different properties you can set on the actual bar and on a UINavigationItem.

Just keep in mind:

  1. You lose back button functionality (unless you wire it yourself)
  2. The built-in "drag from the left-hand side to swipe back" gesture is forfeited

UINavigationController's are your friends.

Ben Reed
  • 824
  • 1
  • 8
  • 26
5

If you wanted to change the title from a child view controller of a Page View Controller that's embedded in a navigation controller, it would look like this:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.parent?.title = "some title"
}
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
4

Swift 3

I created an outlet for the navigation title bar item that comes with the navigation bar (from the Object Browser) in the storyboard. Then I sued the line below:

navigationBarTitleItem.title = "Hello Bar"
nyxee
  • 2,773
  • 26
  • 22
4

If you have not created navigation bar in your view controller from storyboard this will work.

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "Title"
}

If you have created navigation bar in your view controller from storyboard this will be helpful.

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "Title"
}
Nisarg Shah
  • 724
  • 5
  • 12
2

Swift 5.1

// Set NavigationBar Title Programmatically and Dynamic

Note : First add NavigationControllerItem to Your ViewController then goto their ViewController.swift file and Just Copy and Paste this in viewDidLoad().

navigationItem.title = "Your Title Here"

e.g.

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "Your Title Here"
}
Raksha.
  • 479
  • 6
  • 6
2

This worked for me. Just connect an outlet to your nav bar and access the title through the topItem

navBar.topItem?.title = "Your Title Here"
Jordan
  • 127
  • 1
  • 1
  • 9
1

I prefer using self.navigationItem.title = "Your Title Here" over self.title = "Your Title Here" to provide title in the navigation bar since tab bar also uses self.title to alter its title. You should try the following code once.

Note: calling the super view lifecycle is necessary before you do any stuffs.

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavBar()
    }
}

private func setupNavBar() {
    self.navigationItem.title = "Your Title Here"
}
kalehmann
  • 4,821
  • 6
  • 26
  • 36
Mukesh Shakya
  • 425
  • 3
  • 9
1

in viewDidLoad

navigationController?.navigationBar.topItem?.title = "Your Text"

  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Feb 15 '22 at 16:50
0

In Swift 4:

Swift 4

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Your title"
}

I hope it helps, regards!

Radames E. Hernandez
  • 4,235
  • 27
  • 37
  • 3
    This is just half of the accepted answer. Also, if you say that nothing changed, then why would you post anything?! Please delete. – Eric Aya Oct 03 '18 at 09:36
  • @Moritz are you moderator or something like that? if say Yes, I will delete it, if not I will not delete it. – Radames E. Hernandez Oct 03 '18 at 17:23
  • 1
    You really say it ?, above me there are 4 completely equal answers, then why do you come with me and tell me to erase mine? I am a trusted user who supports the community, I am not going around just repeating answers. I just wanted to update this question with an updated swift version. – Radames E. Hernandez Oct 04 '18 at 15:20
  • 4
    There’s nothing updated in your answer. This is the same code as the accepted answer, the “Normally this is done in view did load” part... – Eric Aya Oct 05 '18 at 06:40
-1

In viewWillAppear()

self.title = "Title here"

or you may try this also

if you have navigation controller

navigationItem.title = "Title here"