Programmatically setting tabBarItem title in Swift

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..

  • UIPopoverController too large and UIPickerView too small
  • Obj-C CocoaPods + Swift Framework
  • Is there a way to test my Xcode 7.2-compiled app with iOS 9.3?
  • How to call Objective-C instancetype method in Swift?
  • ABMultiValueGetCount - kABPersonSocialProfileProperty always returns 0 ? How can I get Facebook details from address book?
  • Xcode 9 / iOS 11 “CALayer bounds contains NaN: ” when popping view controller with nested UINavigationController and UITabBarController
  • 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?

    4 Solutions Collect From Internet About “Programmatically setting tabBarItem title in Swift”

    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"
    

    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.

    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.

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