Swift- Navigation Controller和Tab Bar Controller以编程方式

时间:2019-04-17 18:26:31

标签: ios swift xcode uinavigationcontroller

我正在以编程方式使用标签栏和导航控制器对应用程序进行编码,删除了情节提要,并且当“ secondViewController”不是mainNavigationController.viewControllers数组的一部分时,该应用程序也可以正常运行并显示导航栏,但是,当我添加secondViewController时,导航栏不会显示在视图上。没有显示导航栏,我该怎么办?我应该将mainNavigationController改为rootViewController吗?

[SQLServer]
Driver              = MySQL Driver
Database            = db
Servername          = ip
UserName            = user
Password            = pwd
Port                = port

1 个答案:

答案 0 :(得分:0)

尝试

let mainNavigationController = MainNavigationController()
let secondNavigationController = UINavigationController(rootViewController: secondViewController)
let mainTabBarController = UITabBarController()
let mainViewController = MainViewController()
let secondViewController = SecondViewController()

mainNavigationController.title = "Single Meal"
secondViewController.title = "Group Meal"
mainViewController.title = "Single Meal"

mainNavigationController.viewControllers = [mainViewController, secondViewController]

mainTabBarController.viewController = [mainNavigationController, secondNavigationController]

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = mainTabBarController

以您的方式选择第二个选项卡时,secondViewController实际上未嵌入在导航控制器中,但是当您使用推送导航控制器从mainViewController转到secondViewController时,第二个视图控制器显示在mainViewController的顶部