uinavigationcontroller - How to set title of Navigation Bar in Swift?

ID : 274359

viewed : 32

Tags : swiftuinavigationcontrolleruitabbarcontrollerswift





Top 3 Answer for uinavigationcontroller - How to set title of Navigation Bar in Swift?

vote vote

94

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.

vote vote

82

Your question is unclear, if the code you presented is from UIViewController that is kept inside UINavigationController and navigationBar is shown you can simply use:

override func viewDidLoad() {     super.viewDidLoad()     self.title = "My Title" } 
vote vote

77

In UINavigationController Class Reference Apple wrote this:

init(rootViewController: UIViewController)
Parameters rootViewController
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.

But you can do it if you insisted.But in this case , your ViewControllers of tabbarController all shared the same title,which is the title of tabbarController.

A better way to do it is to give each of tabbarController's viewControllers a NavigationViewController.

Top 3 video Explaining uinavigationcontroller - How to set title of Navigation Bar in Swift?







Related QUESTION?