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
Try the following in viewDidLoad
self.navigationItem.title = "Your Title"
If you have a NavigationController
embedded inside of a TabBarController
see below:
super.tabBarController?.title = "foobar"
You can debug issues like this with debugger scripts. Try Chisel's pvc
command to print every visible / hidden view on the hierarchy.
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"
Swift 5.1
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "What ever you want"
}
The code below works for me with Xcode 7:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Your Title"
}
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"
}