ios - Swift-以编程方式导航 Controller 和选项卡栏 Controller

标签 ios swift xcode uinavigationcontroller

我正在以编程方式编写一个带有选项卡栏和导航 Controller 的应用程序,我已经删除了 Storyboard,该应用程序也运行良好,并且当“secondViewController”不是 mainNavigationController.viewControllers 数组的一部分时显示导航栏,但是,当我添加 secondaryViewController 时,导航栏不会显示在 View 上。我可能做错了什么而没有显示导航栏?我应该将 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

最佳答案

试试这个

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 实际上并未嵌入导航 Controller 中,但是当您使用导航 Controller 上的推送从 mainViewController 转到 secondaryViewController 时,第二个 View Controller 将显示在 mainViewController 的顶部

关于ios - Swift-以编程方式导航 Controller 和选项卡栏 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55733871/

相关文章:

ios - NSXML Parser 跳过 & In 字符串后的剩余字符串

ios - 在 swift、var 和 func 中获取值(value)的首选方式是什么?

ios - 在 Facebook 上分享最佳成绩

ios - 解析字符串并在 swift 中以 Json 形式发送

iphone - 为 ipad 复制 xib 后 GUI 不正确?

ios - 在 Swift 中更改我的 @IBAction 按钮的外观

ios - ipa大小和项目大小之间的巨大差异

iphone - UIView 宽高颠倒

ios - 如何从M3U8文件下载视频?

ios - 在 iOS 模拟器上测试 FCM 通知