0
votes

I am programmatically coding an app with a tab bar and a navigation controller, I've deleted the storyboard, the app also runs fine and shows the navigation bar when the 'secondViewController' is not part of the mainNavigationController.viewControllers array, however when I add the secondViewController, the navigation bar doesn't show up on the view. What could I be doing wrong that isn't showing the navigation bar? Should I be making the mainNavigationController the rootViewController instead?

    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
1
A little unclear... With the code you've shown, you start with 2 tabs - tab 1 shows a Navigation Controller (with nav bar), starting at mainViewController and you can push-to and pop-from secondViewController... and tab 2 shows just secondViewController which would, of course, not show a nav bar. Is that not what you're getting?DonMag
It's fairly straightforward to programmatically create a UINavigationController and push/pop between two view controllers. Why not start there and then add your UITabBarController? That way you can - at least - narrow down the specific issue you are facing. (It would also clarify things to us too.)dfd

1 Answers

0
votes

try this

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

in your way when you are selecting the second tab then secondViewController is actually not embedded in navigation controller but when you go to secondViewController from mainViewController using push on navigation controller then second view controller is shown on the top of mainViewController