iosswiftuiviewcontrolleruinavigationcontrolleruitabbarcontroller

Tabbar won't hide when pushed into a ViewController inside a UITabBarController


For the purpose of this question, I'm showing a stripped down version of my view hierarchy. My app contains a UITabBarController as the base. Each tab's top most view controller is a navigation controller and it has view controllers embedded in each of them.

enter image description here

Let's take the first tab.

UITabBarController -> UINavigationController -> UITableViewController -> UIViewController

Let's say the UITableViewController instance is some sort of a list and the UIViewController is the detail view. When the user taps on an item from the list, it takes you to the detail view. And when that happens I have set the UIViewController's hidesBottomBarWhenPushed property to true so that the tabbar at the bottom would hide when the user is in the detail view.

enter image description here enter image description here

My app receives push notifications. When tapped on them, it should open directly into the detail view. I can get it to navigate there. But the issue is the tabbar at the bottom is still visible!

enter image description here

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    window = UIWindow(frame: UIScreen.main.bounds)

    let tabBarController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController

    if openingFromPush {
        let firstNavigationController = storyboard.instantiateViewController(withIdentifier: "FirstNavigationController") as! UINavigationController
        let tableViewController = storyboard.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController
        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        viewController.hidesBottomBarWhenPushed = true
        firstNavigationController.viewControllers = [tableViewController, viewController]

        tabBarController.viewControllers?[0] = firstNavigationController
        // tabBarController.tabBar.isHidden = true
        window?.rootViewController = tabBarController
    } else {
        window?.rootViewController = tabBarController
    }

    window?.makeKeyAndVisible()
    return true
}

I set that same hidesBottomBarWhenPushed property to true in the when I instantiate the view controller but that doesn't seem to have any effect. I even tried straight up hiding the tabbar like this tabBarController.tabBar.isHidden = true but that doesn't do anything at all either.

I can't figure how how to resolve this. Any help would be appreciated.

I attached a sample Xcode project here as well if that helps.


Solution

  • You can use this code for pushing detail view controller:

     if openingFromPush {
                let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
                viewController.hidesBottomBarWhenPushed = true
                if let nvc = tabBarController.viewControllers?[0] as? UINavigationController {
                    nvc.pushViewController(viewController, animated: false)
                }
    
                window?.rootViewController = tabBarController
            }
    

    You don't need to init navigation view controller and table view controller again its already inside tab bar controller