34

I have four UIViewControllers that are linked to a UITabBarController's tab bar. I need to set the tab bar item titles outside of the storyboard, and inside of their classes.

I've tried..

class MyViewController: UIViewController {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.title = NSLocalizedString(MyConstants.StringKeys.TabName, tableName: Constants.Strings.MyTable, comment: Constants.EmptyString);
    }
}

This is called, but the title is never set. Same with self.tabBarItem.title = "the title"

I've also tried setting the title in viewDidLoad, but that only updates the title after going to the view controller.

Thoughts?

Sam
  • 1,042
  • 2
  • 8
  • 19

7 Answers7

42

You can set the tab titles in the view controllers themselves in viewDidLoad by setting the view controller's title property.

title = "Number 0"

Alternatively, if want to set the titles from your tab bar controller, you can set them like this in your tab bar controller's viewDidLoad:

tabBar.items?[0].title = "Number 0"
tabBar.items?[1].title = "Number 1"
keithbhunter
  • 12,258
  • 4
  • 33
  • 58
24

I figured it out, looks like it was being over written by awakeFromNib().

override func awakeFromNib() {
    super.awakeFromNib()

    self.title = NSLocalizedString(MyConstants.StringKeys.TabName, tableName: Constants.Strings.MyTable, comment: Constants.EmptyString);
}

I moved my self.title assignment there and it corrected my issue.

Sam
  • 1,042
  • 2
  • 8
  • 19
14

I've been trying different solutions but the only one what worked for me was adding the tab bar set up the code in the viewWillAppear method in the UITabBarController. I don't do it in each view controller individually because it works only when the tab bar button is pressed:

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

    guard let items = tabBar.items else { return }

    items[0].title = "Title0"
    items[1].title = "Title1"
    items[2].title = "Title2"
    items[3].title = "Title3"
}
Ginés SM
  • 233
  • 2
  • 5
  • 1
    This works for me. I tried both viewDidLoad() and awakeFromNib() but not working from there. By the way, I don't think guard ... is good. You do want to get exception if failed to set titles for 3 items. – David.Chu.ca Dec 03 '17 at 05:08
  • This works for me.. viewDidLoad is not working.. – chengsam Jan 03 '22 at 10:39
8

Here is the solution in every viewController.swift file you can just add the following code

    override func awakeFromNib() {
    self.tabBarItem.title = "title"
    self.tabBarItem.image = "image.png"
}

That's a super easy awakeFromNib method and you can easily call any thing when nib has created simple on start because in ViewDidLoad or any viewControllerDelegate method called when you clicked or select. So that's a super easy function.

Thanks.

Poran Ramen
  • 123
  • 2
  • 8
3

In ViewDidLoad on first Viewcontroller do this:

override func viewDidLoad() {

...

    self.tabBarController?.viewControllers?[1].tabBarItem.title = NSLocalizedString("Home", comment: "")
    self.tabBarController?.viewControllers?[2].tabBarItem.title = NSLocalizedString("Tab 1", comment: "")
    self.tabBarController?.viewControllers?[3].tabBarItem.title = NSLocalizedString("Tab 2", comment: "")
    self.tabBarController?.viewControllers?[4].tabBarItem.title = NSLocalizedString("Tab 3", comment: "")

...
}
Mike Zriel
  • 1,575
  • 1
  • 17
  • 28
2

In Swift 5:

Create Custom Tabbar class.

In ViewDidLoad add:

 self.viewControllers?[0].tabBarItem.title = NSLocalizedString("Home", comment: "")
 self.viewControllers?[1].tabBarItem.title = NSLocalizedString("Action", comment: "")
 self.viewControllers?[2].tabBarItem.title = NSLocalizedString("Find LifePass", comment: "")
Anil Kumar
  • 1,830
  • 15
  • 24
1

If you create your ViewControllers programmatically before putting to the TabBarController, try this:

override init(style: UITableViewStyle) {
    super.init(style: style)

    self.title = "Title"
}

or this, if it is not a TableViewController:

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

    self.title = "Title"
}
Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70