RyJ RyJ - 4 months ago 17
iOS Question

iOS: Tab bar item title before the tab is selected

In iOS 9, the view controller that is displayed in a tab is responsible for it's

tabBarItem
. But a tab bar controller only loads the view controllers for each tab as they are needed. So initially, it only loads the view controller for tab 1 (if the app launches on the first tab). Therefore, unless you're setting the title for each tab item in a storyboard, only the first tab displays a title since it is the only view controller loaded at that time, while the rest only show their icon (which I am doing through a storyboard since the icon doesn't need to be localized).

How can you set the title for all tabs without doing it through a storyboard?

Answer

You can put all the titles into array, and preset the tabBarItem title:

if let tabTitles = self.tabBarController?.tabBar.items as? [UITabBarItem]
    {
        tabTitles[0].title = "Messages"
        tabTitles[1].title = "Contacts"
    }
Comments