在我的应用程序中,我有一个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]
}
}
所以我的问题是:如何在视图控制器的顶部放置标题?
在之前的代码中我试过,但它不起作用!
像这样的:
最佳答案
当视图控制器包装在title
中时,将显示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/