Rising
Rising

Reputation: 2555

Changing navigation title programmatically

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.

Upvotes: 231

Views: 287163

Answers (18)

Pratik Gajera
Pratik Gajera

Reputation: 21

In viewWillAppear()

self.title = "Title here"

or you may try this also

if you have navigation controller

navigationItem.title = "Title here"

Upvotes: 1

Raksha.
Raksha.

Reputation: 519

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"
}

Upvotes: 3

Jordan
Jordan

Reputation: 177

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"

Upvotes: 2

A. Deliaslan
A. Deliaslan

Reputation: 37

in viewDidLoad

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

Upvotes: 1

Nisarg Shah
Nisarg Shah

Reputation: 754

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"
}

Upvotes: 4

Marlhex
Marlhex

Reputation: 1989

Swift 5.1

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "What ever you want"
}

Upvotes: 10

Francojamesfan
Francojamesfan

Reputation: 105

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"
}

Upvotes: 5

Mukesh Shakya
Mukesh Shakya

Reputation: 445

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"
}

Upvotes: 1

rustyMagnet
rustyMagnet

Reputation: 4125

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.

Upvotes: 7

Radames E. Hernandez
Radames E. Hernandez

Reputation: 4425

In Swift 4:

Swift 4

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Your title"
}

I hope it helps, regards!

Upvotes: 0

grmonkeybiz
grmonkeybiz

Reputation: 211

The code below works for me with Xcode 7:

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

Upvotes: 21

Avi
Avi

Reputation: 2216

Try the following in viewDidLoad

self.navigationItem.title = "Your Title"

Upvotes: 84

Kevin Singh
Kevin Singh

Reputation: 421

The correct answer for people using would be

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Your Text"
}

Upvotes: 6

nyxee
nyxee

Reputation: 2811

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"

Upvotes: 4

Sirop4ik
Sirop4ik

Reputation: 5263

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);
}

Upvotes: 6

AJ9
AJ9

Reputation: 1276

I found this to work:

navigationItem.title = "Title"

Upvotes: 14

Ben Reed
Ben Reed

Reputation: 884

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.

Upvotes: 5

drewag
drewag

Reputation: 94773

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"

Upvotes: 621

Related Questions