与导航控制器一起使用时不显示 UITabBarItem 的标题

UITabBarItem's title does not shown when using with Navigation controller

我在使用非常简单的应用程序时遇到了问题。

我的应用程序只有 2 个主要 UIViewControllers,分别称为新闻和类别,每个都有自己的 UINavigationController

所以在AppDelegate.swift,我是这样做的

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: [=10=])
}
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()

当我 运行 这个简单的应用程序时,标签栏项目不显示任何内容:(

但是当我将 UITabBarItem 更改为这样的系统样式时

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

一切正常!太难懂了!

那么有人知道为什么我的 title-only 标签栏项目不起作用吗?我错过了什么重要的事情吗?

提前致谢!

title 属性 添加到 ViewController 以在 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"

.....

或者

将图像分配给 UITabBarItem 以查看结果。

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

....

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

.....

更新:

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.