Julian Silvestri
Julian Silvestri

Reputation: 2037

Cannot set tab bar items text programmatically on tabBarControllerVC

I have a tab bar controller that has storyboard references linking the appropriate storyboard. I am having trouble localizing the tab bar titles. Currently the tab bar titles are set on the storyboard only. When I clear the titles for the tab bar items in the story board and attempt to set them in code below.

mainTabBar.items![0].title = NSLocalizedString("mainTabBar_tab0", comment: "")
mainTabBar.items![1].title = NSLocalizedString("mainTabBar_tab1", comment: "")
mainTabBar.items![2].title = NSLocalizedString("mainTabBar_tab2", comment: "")
mainTabBar.items![3].title = NSLocalizedString("mainTabBar_tab3", comment: "")
mainTabBar.items![4].title = NSLocalizedString("mainTabBar_tab4", comment: "")

This yields inconclusive results and the tab bar items remain blank.

Below is a screenshot of how I have my tab bar items established.

enter image description here

EDIT---Adding Localized Files

"mainTabBar_tab0" = "AA";
"mainTabBar_tab1" = "BB";
"mainTabBar_tab2" = "CC";
"mainTabBar_tab3" = "DD";
"mainTabBar_tab4" = "EE";

I am calling the tabBar items title code inside the main tab bar controller as shown in picture.

Upvotes: 0

Views: 369

Answers (1)

Could you try setting each ViewController title in their corresponding viewWillAppear? Something like this:

self.tabBarController?.tabBar.items?[0].title = NSLocalizedString("mainTabBar_tab0", comment: "comment")

Hope it helps

Upvotes: 1

Related Questions