简体   繁体   中英

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 :

使用需要用到这个属性:

self.navigationItem.title = "someTitle"

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"
}

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

将 UINavigationController 嵌入您的故事板并放置:

navigationBar.topItem.title = "Nav title"

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"
}

For Swift 3+

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

    self.navigationItem.title = "Title"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM