ios 双导航栏显示与标签栏控制器编程编码

np8igboo  于 2023-05-02  发布在  iOS
关注(0)|答案(1)|浏览(105)

我最近一直在将我的代码从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: $0)}
    }

}

这是选项卡栏控制器,它将选项卡栏和导航栏(我假设)添加到每个视图控制器。我知道这是最后一行添加根视图控制器到导航栏,但我不太确定是什么原因导致导航控制器上的双重(见图片发生了什么)Image showing the issue with the double navigation controller
谢谢!

lztngnrs

lztngnrs1#

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

viewControllers = tabBarList.map { UINavigationController(rootViewController: $0)}

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

相关问题