显示带有以编程方式编码的选项卡栏控制器的双导航栏

Double navigation bar showing up with tab bar controller programmatically coded

我最近一直在将我的代码从 Xcode 标准情节提要模式更改为编程编码版本,这是一个学习曲线,但是我现在对在顶部设置导航栏感到困惑以及底部的标签栏控制器。我在网上学习了很多教程,但遇到了问题,在以编程方式编码时,屏幕顶部有一个带有选项卡栏控制器的双导航栏。

我知道这通常意味着导航栏已被声明两次,但我没有这样做,因为它依赖于标签栏代码来提供导航栏。与此同时,我尝试在视图控制器的 viewDidLoad 函数中声明一个导航栏,但是这什么也没做,即使与 GUI 的初始化配对也是如此。

我目前的标签栏控制器和导航控制器代码是:

class TabBar: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //setting up the variable for the first view controller which will be used for the tab bar
        let firstViewController = MapVC()
        //set the nav title
        firstViewController.title = "Home"
        //initialising the first tab bar item, which will have the title of Home, the image named below and the tag number, showing the position on the bar
        firstViewController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "Home.png"), tag: 0)
        //initialising the second of the view controllers which will be used to access from the tab bar controller
        let secondViewController = localReportsTVC()
        //set the nav title
        secondViewController.title = "Local Reports"
        //setting the second view controller on the tab bar and giving it a title, image and location on the bar
        secondViewController.tabBarItem = UITabBarItem(title: "Local Reports", image: UIImage(named: "Local.png"), tag: 1)
        //setting up the third view controller to be referenced on the tab bar controller
        let thirdVC = NewReportScreen()
        //set the nav title
        thirdVC.title = "New"
        //setting the third view conteroller to be on the tab bar with the image, name and the location on the bar in relation to the other items
        thirdVC.tabBarItem = UITabBarItem(title: "New Report", image: UIImage(named: "Plus Icon.png"), tag: 2)
        //setting up the third view controller to be referenced in the tab bar controller
        let fourthVC = MyReportsTVC()
        //set the nav title
        fourthVC.title = "My Reports"
        //setting the third item on the tab bar up so that it has a position, image and title
        fourthVC.tabBarItem = UITabBarItem(title: "My Reports", image: UIImage(named: "MyReports.png"), tag: 3)
        //setting up the fifth section of the tab bar, where it will be referenced later
        let fithVC = SettingsScreen()
        //set the nav title
        fithVC.title = "Settings"
        //setting up the fifth item, so that it has a title, image and position on the bar
        fithVC.tabBarItem = UITabBarItem(title: "Settings", image: UIImage(named: "Settings Icon.png"), tag: 4)
        //initialising the final tab bar wih all of the elements from above
        let tabBarList = [firstViewController, secondViewController, thirdVC, fourthVC, fithVC]
        //setting the view controllers equal to the tab bar list defined above - also adding in the navigation controller to each of the tabs so that they have a title and also a navigation controller to add the back button in
        viewControllers = tabBarList.map { UINavigationController(rootViewController: [=10=])}
    }

}

这是标签栏控制器,它将标签栏和导航栏(我假设)添加到每个视图控制器。我知道这是将根视图控制器添加到导航栏的最后一行,但是我不太确定是什么导致导航控制器加倍(有关发生的情况,请参见图片)Image showing the issue with the double navigation controller

谢谢!

如果在 TabbarController 之前你有一个 NavigationController 并且当你设置

时,NavigationController 将被复制
viewControllers = tabBarList.map { UINavigationController(rootViewController: [=10=])}

您创建另一个 NavigationController。 这就是为什么 :)