在我的应用程序中,我有一个 tabBarViewController,这是第一个栏:
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
override func viewWillAppear(_ animated: Bool) {
self.navigationItem.title = "RECENTS"
}
}
这是自定义类:
class CustomTabBar: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let firstVC = FirstViewController()
let secondVC = SecondViewController()
let thirdVC = ThirdViewController()
firstVC.tabBarItem.title = "RECENT"
secondVC.tabBarItem.title = "MAP"
thirdVC.tabBarItem.title = "SETTINGS"
firstVC.title = "RECENT"
secondVC.title = "MAP"
thirdVC.title = "SETTINGS"
viewControllers = [firstVC,secondVC,thirdVC]
}
}
所以我的问题是:如何将标题放在 View Controller 的顶部? 在之前的代码中我试过了,但它不起作用!
最佳答案
title
属性在 View Controller 包装在 UINavigationController
中时显示。这里有一些代码替换了你示例中的最后一行(即 viewControllers
分配),应该可以解决问题:
let firstNC = UINavigationController(rootViewController: firstVC)
let secondNC = UINavigationController(rootViewController: secondVC)
let thirdNC = UINavigationController(rootViewController: thirdVC)
viewControllers = [firstNC,secondNC,thirdNC]
关于ios - 如何在 ViewController 顶部设置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48389292/