【问题标题】:Swift- Navigation Controller and Tab Bar Controller ProgrammaticallySwift-以编程方式导航控制器和标签栏控制器
【发布时间】:2019-04-17 18:26:31
【问题描述】:

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

    let mainNavigationController = MainNavigationController()
    let mainTabBarController = MainTabBarController()
    let mainViewController = MainViewController()
    let secondViewController = SecondViewController()

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

    mainNavigationController.viewControllers = [mainViewController, secondViewController]
    mainTabBarController.viewControllers = [mainNavigationController, secondViewController]

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

【问题讨论】:

  • 有点不清楚...使用您显示的代码,您从 2 个选项卡开始 - 选项卡 1 显示一个导航控制器(带导航栏),从 mainViewController 开始,您可以推送- to and pop-from secondViewController... 和 tab 2 只显示 secondViewController 当然,not 显示导航栏。这不是你得到的吗?
  • 以编程方式创建UINavigationController 并在两个视图控制器之间推送/弹出非常简单。为什么不从那里开始,然后添加您的UITabBarController?这样,您至少可以缩小您面临的具体问题的范围。 (它也会向我们澄清一些事情。)

标签: ios swift xcode uinavigationcontroller


【解决方案1】:

试试这个

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 的顶部

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多