15

So I have a basic view controller with a navigation bar, this is view controller B, so I'm performing a segue to go here, and I'm trying to change the title like this:

override func viewDidLoad() {
    debugPrint(self.selectedName)
    super.viewDidLoad();
    self.navigationItem.title = "A NEW TITLE"
}

But it doesn't do anything

enter image description here

11
  • can you add your storyboard scene Commented Jan 20, 2017 at 13:06
  • Your screen shot is from the storyboard. The code will only affect the screen when you actually run it. Or just update the label directly in the storyboard. Commented Jan 20, 2017 at 13:06
  • 1
    Is your view controller embedded in a navigation controller or did you simply add a navigation bar object to your view? Commented Jan 20, 2017 at 13:08
  • seconded with @xoudini, i think there is another outlet Commented Jan 20, 2017 at 13:08
  • @Fogmeister updated Commented Jan 20, 2017 at 13:09

5 Answers 5

19

To set Title on NavigationBar simply we can do by below code

self.title = "Your Title"
Sign up to request clarification or add additional context in comments.

1 Comment

I have an issue where this Simply doesn't work on iOS 16 building with Xcode 14 cant find any hint of a solution yet.
16

For Swift 3:

If you want to set just the navigation title without using a UINavigationController then make an outlet of the navigation item as

 @IBOutlet weak var navItem: UINavigationItem!

and then in viewDidLoad() write

navItem.title = "ANY TITLE"

Comments

12

There are plenty of methods which you can follow to achieve this. Here is few of those.

First things first.

If you are ready to "Embed in" a navigation controller or if you have already one then you can access that using following code.

self.navigationController?.navigationBar.topItem?.title = "Your Title"

Now for more customization:

  1. Place a UINavigationBar object on ViewController scene and add constraints to it.
  2. Make an outlet of newly placed UINavigationBar like as follows -

    @IBOutlet weak var orderStatusNavigationbar: UINavigationBar!
    
  3. Now set the title using the outlet.

    orderStatusNavigationbar.topItem?.title = "Your Title"
    

All this above code are in Swift 3 but will work on lower versions of Swift also(atleast on Swift 2.0)

Hope this helped.

1 Comment

Instead of the accepted answer I used this solution and it works perfectly!
4

Swift 4 / XCode 10

  1. Add new NavigationBar -> Drap and Drop it to your view

  2. Press CTRL to add new Outlet Action

  3. Example : @IBOutlet weak var main_navbar: UINavigationBar in ViewController class.

  4. Then set the title : main_navbar.topItem?.title = "YOUR TITLE"

This solution worked for me, hope it does for you too. - rustenter image description here

Comments

3

Ok, So you need to embed the whole thing in a Navigation Controller first, and then make that navigation controller as the initial controller.

Select your storyboard, click the first controller then click this -

img

Then you remove the navigation bar you set(put) over the last controller named "title".

The reason this didnt work, as you are trying to change the title of the navigation controller's navigation bar, but it doesnt have it, hence it cant change it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.