1
votes

I have recently been changing my code from the Xcode standard storyboard mode to the programmatically coded version, which has been a learning curve, however I am now stuck with regards to having a navigation bar at the top as well as a tab bar controller at the bottom. I have followed many tutorials online and have gotten myself stuck, with there being a double navigation bar at the top of the screens with a tab bar controller when programmatically coding it.

I am aware that this usually means that the navigation bar has been declared twice, however I haven't done this as it is relying on the tab bar code to also provide the navigation bar. Along with this, I tried to declare a navigation bar in the view controller's viewDidLoad function, however this did nothing, even when paired with the initialisation of the GUI.

The code I have at the moment for the tab bar controller and navigation controller is:

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)}
    }

}

This is the tab bar controller, which adds the tab bar and navigation bar (I assume) to every view controller. I understand it is the last line adding the root view controller to the navigation bar, however I'm not too sure what is causing the double up on navigation controller (see image for what is happening) Image showing the issue with the double navigation controller

Thanks!

1
Inside SceneDelegate are you instantiating a navigation controller?aheze
In the Scene delegate I have got it to decide between a log in screen and the main home screen, where it does instantiate another Navigation controller, so there is the issue - but I'm trying to figure out why that wasn't showing up before when the pages were set up.Alex

1 Answers

2
votes

The NavigationController will be duplicated if before TabbarController you have a NavigationController and when you set

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

You create another NavigationController. That's why :)