title = @"title"; But If you have a standalone navigation bar that is created programmatically within an object of UIViewController, You have to set the initial appearance of the navigation bar by creating the appropriate UINavigationItem objects and adding them to the navigation bar object stack i.e.
Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.
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"
Try the following in viewDidLoad
self.navigationItem.title = "Your Title"
The code below works for me with Xcode 7:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Your Title"
}
I found this to work:
navigationItem.title = "Title"
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);
}
The correct answer for people using swift4 would be
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Your Text"
}
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:
UINavigationController
's are your friends.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With