ios - UITabBarController 中的 UINavigationController 未完全包装 UIViewController

标签 ios swift uinavigationcontroller uitabbarcontroller

我有一个 UITabBarController 作为我的 rootViewcontroller,每个选项卡有 3 个 UINavigationController。每个UINavigationController都有一个初始的UIViewController,它只有红色背景色。 我的问题是 UINavigationController 在第一次启动时没有完全覆盖 UIViewController 。切换选项卡后,它会覆盖 UIViewController。那么我在这里做错了什么?

提前致谢。

应用程序委托(delegate):

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

UITabBarController:

class TabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Navigation Tab
        let navVC = NavigationViewController()

        // Departure Tab
        let depVC = DeparturesViewController()

        // Settings Tab
        let setVC = SettingsViewController()

        self.viewControllers = [
            createNavigationController(title: "Navigation", rootViewController: navVC, imageName: "map"),
            createNavigationController(title: "Abfahrten", rootViewController: depVC, imageName: "station"),
            createNavigationController(title: "Einstellungen", rootViewController: setVC, imageName: "user"),
        ]        
    }

    private func createNavigationController(title: String, rootViewController: UIViewController, imageName: String) -> UINavigationController {

        rootViewController.title = title
        let nc = UINavigationController(rootViewController: rootViewController)
        nc.title = title
        nc.view.backgroundColor = .white
        nc.navigationBar.prefersLargeTitles = true
        nc.navigationController?.navigationItem.largeTitleDisplayMode = .always
        nc.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
        return nc
    }

    override func viewWillAppear(_ animated: Bool) {
        self.selectedIndex = 0
    }

}

非常简单的 UIViewController:

class NavigationViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .red
    }

}

enter image description here

最佳答案

从 TabBarController 中删除以下代码后,它完全包装了 Viewcontroller

override func viewWillAppear(_ animated: Bool) {
    self.selectedIndex = 0
}

但我无法解释为什么

关于ios - UITabBarController 中的 UINavigationController 未完全包装 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904450/

相关文章:

ios - ComponentKit 与 AsyncdisplayKit

ios - 摆脱空的 JSON 数据 iOS 应用程序

ios - Swift:如果让多个单元类

ios - CloudKit "subscription is duplicate of"错误 - 获取 SubscriptionId

iOS 和 Parse 抛出异常 : "unexpectedly found nil"

swift - UINavigationController(rootViewController : MyViewController()) triggers viewdidLoad and returns nil navigationController

ios - UICollectionView 状态恢复 : restore all UICollectionViewCells

Swift - 如何正确获取 CPU 负载

ios - 动画并不总是从 UIViewControllerAnimatedTransitioning 中的 animateTransition 可见

ios - iOS 导航 Controller 代码的正确位置?