6

If I set the tab bar item title in the storyboard or from code (see below), it is changed to that tab's view controller title when I tap the tab and the controller loads. I don't want this.

I want the tab bar item title that I set in the storyboard or from code to remain, without being changed. How do I achieve that?

I still want to set the controller title for the UINavigationController to show on back buttons etc.

override func viewDidLoad() {
    super.viewDidLoad()

    tabBarItem.title = "Keep me"

    // But this overrides it, which I don't want.
    title = "Don't let me change the tab bar title"
}
Henrik N
  • 15,786
  • 5
  • 82
  • 131
  • Possible duplicate of [how to change uiviewcontroller title independent of tabbar item title](https://stackoverflow.com/questions/21615637/how-to-change-uiviewcontroller-title-independent-of-tabbar-item-title) – HHK Nov 22 '17 at 09:55

1 Answers1

11

you can change

title = "Don't let me change the tab bar title"

to

navigationItem.title = "Don't let me change the tab bar title"

edit:

override var title: String? {
    didSet{
        tabBarItem.title = "you want"
    }
}
NF Lee
  • 462
  • 3
  • 12
  • Thank you very much. This was a simplified example – in real life, the view controller under that tab uses [Turbolinks](github.com/turbolinks/turbolinks-ios) which sets the title. But I guess that we could then tweak Turbolinks and make sure it sets only the navigationItem title. – Henrik N Oct 10 '17 at 08:23
  • @HenrikN well, you may override the `title` property of the view controller and set `tabBarItem.title` to what you want in `didSet`. – NF Lee Oct 10 '17 at 08:37
  • We ended up overriding Turbolinks to assign `navigationItem`, based on this answer. Thank you! https://github.com/turbolinks/turbolinks-ios/issues/101#issuecomment-335411356 – Henrik N Oct 10 '17 at 09:33