user10733862
user10733862

Reputation:

Getting two navigation bar when using TabBar

I rewrite a TabBarController:

//
//  TabBarController.swift
//  
//
//  Created by Coel on 2019/7/4.
//

import UIKit

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let TrashListVC = TrashListViewController()
        let SearchVC = SearchViewController()
        let SettingsVC = SettingsViewController()
        TrashListVC.tabBarItem = UITabBarItem(tabBarSystemItem: .search, tag: 0)
        SearchVC.tabBarItem = UITabBarItem(tabBarSystemItem: .search, tag: 1)
        SettingsVC.tabBarItem = UITabBarItem(tabBarSystemItem: .more, tag: 2)
        let tabBarList = [SearchVC, TrashListVC, SettingsVC]
        viewControllers = tabBarList.map {
            UINavigationController(rootViewController: $0)
        }
        // Do any additional setup after loading the view.
    }
}

And I call this in AppDelegate:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UINavigationController(rootViewController: TabBarController())
        window?.makeKeyAndVisible()
        return true
    }
}

This result a double Navigation bar like this:

enter image description here (Sorry it's in Chinese)
I try to add self.navigationController?.isNavigationBarHidden = true in TabBarController.swift but not working.

Upvotes: 1

Views: 810

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You have 2 navigations one for the main tabBar and the other for every child vc to hide both of them

1-

let nav = UINavigationController(rootViewController: TabBarController())
nav.isNavigationBarHidden = true
window?.rootViewController = nav

or only

window?.rootViewController = TabBarController()

2-

viewControllers = tabBarList.map { item in 
   let nav =  UINavigationController(rootViewController:item)
   nav.isNavigationBarHidden = true
   return nav
}

Upvotes: 0

matt
matt

Reputation: 535138

You are saying

window?.rootViewController = 
    UINavigationController(rootViewController: TabBarController())

So you are wrapping a navigation controller around your tab bar controller.

But then you are saying:

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

So you are wrapping a navigation controller around every one of your tab bar controller's individual children.

Thus, for every child of the tab bar controller, it has two navigation controllers around it, once for each time you see the words "UINavigationController" in the code I've quoted.

So that's why you see two navigation bars. You have a navigation controller inside a navigation controller.

That architecture is incoherent and forbidden. You may not put a navigation controller, at any depth, inside a navigation controller. You need to rethink your entire architecture.

Upvotes: 3

Related Questions