Procuste
Procuste

Reputation: 61

Swift 2 : Change navigation title on a view related to a Tab Bar Controller

I want to change the navigation's title of a view, which is related to a Tab Bar Controller, which is related to a Navigation Controller (see the picture)

I don't know how to do that.

With a basic view, I just need to do that in the ViewController.swift :self.title="test"

But here, this line changed the tab bar's title, but I want to change the navigation's title.

Main.Storyboard :

Upvotes: 2

Views: 3080

Answers (6)

Woodstock
Woodstock

Reputation: 22926

Use need to use this property:

self.navigationItem.title = "someTitle"

Upvotes: 5

hawkstrider
hawkstrider

Reputation: 4341

According to Apple best practices you should not have a tab bar controller contained inside of a navigation controller, rather you should have the view controller for each tab that requires one to be inside of it's own navigation controller.

There are various issues that can arise from having a tab bar controller contained within a navigation controller.

When implemented according to their standards you can set the title using self.title

An app that uses a tab bar controller can also use navigation controllers in one or more tabs. When combining these two types of view controller in the same user interface, the tab bar controller always acts as the wrapper for the navigation controllers.

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html

Upvotes: 4

muruthi
muruthi

Reputation: 345

On Swift 3, in the UIViewController, override your viewDidAppear method and add this code snippet:

if let tabController = self.parent as? UITabBarController { 
    tabController.navigationItem.title = "My Title"
}

Upvotes: 5

Ali Ihsan URAL
Ali Ihsan URAL

Reputation: 1974

For Swift 3+

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationItem.title = "Title"
}

Upvotes: 0

Almeida
Almeida

Reputation: 1363

I also had difficulty to change a navigation bar title of a child view controller. The solution was:

@IBOutlet weak var navigationBar: UINavigationBar!

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationBar.topItem!.title = "Pickup Address"
}

Upvotes: 0

Jacky Shek
Jacky Shek

Reputation: 955

Embed UINavigationController in your storyboard and put:

navigationBar.topItem.title = "Nav title"

Upvotes: 0

Related Questions