Search code examples
iosswiftuitabbarcontrolleruitabbar

hidesBottomBarWhenPushed hides TabBar forever


I use Xcode 11.2 and the project minimum iOS deployment target is iOS 12.4.

I have a TabBarController on root page and on one of the tabs I have FirstViewController. When I push SecondViewController from FirstViewController, I want the tab bar to be hidden. I used hidesBottomBarWhenPushed property to hide the tab bar.

The tab bar is hidden when I push SecondViewController but when I pop the SecondViewController and move back to FirstViewController, the tab bar is still hidden.

I tried several ways to set hidesBottomBarWhenPushed to false when moving back to FirstViewController but none of the tries worked.

How can I re display tab bar when popped back to FirstViewController?

class FirstViewController: UIViewController {

    @IBAction func buttonTap(_ sender: Any) {
        let vc2 = SecondViewController()

        // Set to Hide TabBar
        hidesBottomBarWhenPushed = true

        navigationController?.pushViewController(vc2, animated: true)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // This Does Not Work
        hidesBottomBarWhenPushed = false
    }
}


class SecondViewController: UIViewController {

    /*
        All The Followings Does Not Work
    */

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        hidesBottomBarWhenPushed = false
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        hidesBottomBarWhenPushed = false
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        hidesBottomBarWhenPushed = false
    }
}

Solution

  • The key was to set hidesBottomBarWhenPushed to true from outside of the SecondViewController.

    The code below was all I needed to write.

    class FirstViewController {
    
        func pushSecondViewController {
            let vc = SecondViewController()
            vc.hidesBottomBarWhenPushed = true // <- Here
            navigationController?.push
            navigationController?.pushViewController(vc, animated: true)
        }
    }