How to Set the Title of a Navigation Bar Programmatically

Changing navigation title programmatically

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"

Set title of navigation bar

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"

How to set the title of a Navigation Bar programmatically?

In your UIViewController

self.navigationItem.title = @"The title";

Changing Nav Bar title programmatically

self.navigationController.navigationBar.topItem.title = @"YourTitle";

UINavigationBar - Set title programmatically?

I've set the title programmatically using code something like this:

navBar.topItem.title = @"title";

where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.

If navBar.topItem is the tab bar item, I don't see a way for you to change the title that appears on the navigation bar without also changing the title on the tab bar item, since the navBar's topItem and the tab bar item is the same object.

Create NavBar programmatically with Button and Title Swift

Updated for Swift 5

Create a navigation item instance and set title and right/left buttons to it. After navigation item is configured add it to the navigation bar.

let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 44))
view.addSubview(navBar)

let navItem = UINavigationItem(title: "SomeTitle")
let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(selectorName:))
navItem.rightBarButtonItem = doneItem

navBar.setItems([navItem], animated: false)

How to set title of Navigation Bar in Swift?]

Since the Tab Bar is actually the Root View Controller of the Navigation Bar, you need to set the UITabBarController's title instead in the viewWillAppear function so that it happens every time you switch tabs:

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "My Title"
self.tabBarController?.navigationItem.leftBarButtonItem = settingsButton //This is the IBOutlet variable that you previously added
}

But a better way to do it would actually be to have it the other way around, like so

Correct view hierarchy

You should hook up a UINavigationController for each child of the UITabBarController, as it is more correct semantically, and simpler to maintain.



Related Topics



Leave a reply



Submit