4

I'm having a trouble with my very simple app.

My app simply have 2 mains UIViewControllers called News and Category, each of them has their own UINavigationController.

So in AppDelegate.swift, I've done like this

window = UIWindow(frame: UIScreen.main.bounds)
let tabBarController = UITabBarController()

let newsVC = NewsVC()

// CREATE TAB BAR ITEM WITH TITLE ONLY
let newsVCTabBarItem = UITabBarItem(title: "News", image: nil, tag: 1)
newsVC.tabBarItem = newsVCTabBarItem

let categoryVC = CategoryVC()

// CREATE TAB BAR ITEM WITH TITLE ONLY
let categoryVCTabBarItem = UITabBarItem(title: "Category", image: nil, tag: 2)
categoryVC.tabBarItem = categoryVCTabBarItem

let rootViewControllers = [newsVC, categoryVC]

// CREATE NAVIGATION CONTROLLER FOR EACH OF THEM
tabBarController.viewControllers = rootViewControllers.map {
    UINavigationController(rootViewController: $0)
}
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()

When I run this simple application, the tab bar items do not show anything :(

But when I change the UITabBarItem to system's styles like this

let newsVCTabBarItem = UITabBarItem(tabBarSystemItem: .featured, tag: 1)

It's working perfectly! So hard to understand!

So does anyone know why my title-only tab bar item does not working? Have I missed something important?

Thanks in advance!

Sateesh Yemireddi
  • 4,289
  • 1
  • 20
  • 37
phuongzzz
  • 85
  • 1
  • 10

1 Answers1

5

Add title property to the ViewControllers to show the title in UITabBarItem.

var title: String? { get set }

Set the title to a human-readable string that describes the view. If the view controller has a valid navigation item or tab-bar item, assigning a value to this property updates the title text of those objects.

let newsVC = ViewController()
newsVC.title = "News"

.....

let categoryVC = ViewController2()
categoryVC.title = "Category"

.....

Or

Assign an image to the UITabBarItem to see the result.

let newsVCTabBarItem = UITabBarItem(title: "News", image: UIImage(named: "news.png"), tag: 1)

....

let categoryVCTabBarItem = UITabBarItem(title: "Category", image: UIImage(named: "category.png"), tag: 2)

.....

Update:

Tab bar items are configured through their corresponding view controller. To associate a tab bar item with a view controller, create a new instance of the UITabBarItem class, configure it appropriately for the view controller, and assign it to the view controller’s tabBarItem property. If you don't provide a custom tab bar item for your view controller, the view controller creates a default item containing no image and the text from the view controller’s title property.

Sateesh Yemireddi
  • 4,289
  • 1
  • 20
  • 37
  • 1
    Cool! It's worked, but I still confusing, if I set the view controller's title different from the ui tab bar item, it show the VC's title (but not the one I've set in ui tab bar item), so the name in ui tab bar item seem useless? – phuongzzz Nov 10 '18 at 16:08
  • 1
    Yes it is. If we do not provide the image for the tabBarItem it takes title property of ViewController. So, the first answer only shows the title of the tabBarItem. If we provide image along with title then UITabBarController considers UITabBarItems. – Sateesh Yemireddi Nov 10 '18 at 16:20
  • 1
    Very glad to help you and even I too forgot why its not working for a moment . – Sateesh Yemireddi Nov 10 '18 at 16:33
  • Any clue with it doesn't show my custom title for the bar when using for example `UITabBarItem(tabBarSystemItem: .featured, tag: 0)`? It shows as tab bar title "Featured" instead of my custom title, despite setting `UIViewController` and `UINavigationController` titles. – piotr_ch May 29 '19 at 17:41