hide / show tab bar when push / back. swift

2019-01-18 15:14发布

问题:

Answer: Use self.tabBarController?.tabBar.hidden instead of hidesBottomBarWhenPushed in each view controller to manage whether the view controller should show a tab bar or not.

override func viewWillAppear(animated: Bool) {
    self.tabBarController?.tabBar.hidden = true/false
} 

I want

view controller 1: tab bar should be showed

view controller 2: tab bar should be showed

view controller 3: tab bar should not be showed.

view controller 4: tab bar should not be showed.

I wrote

// prepareForSegue in view controller 1, 
    let upcoming = segue.destinationViewController as! viewcontroller3
    upcoming.hidesBottomBarWhenPushed = true

// in view controller 3,
    func clickOnButton(button: UIButton) {
        self.hidesBottomBarWhenPushed = false
        self.performSegueWithIdentifier("viewController2", sender: self)
        self.hidesBottomBarWhenPushed = true
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "viewController2" {
            let upcoming = segue.destinationViewController as! viewController2
            upcoming.hidesBottomBarWhenPushed = false
        }
    }
// prepareForSegue in view controller 2
    let upcoming = segue.destinationViewController as! viewController4
    upcoming.hidesBottomBarWhenPushed = true

if 1 -> 3 then back to 1, works.

if 1 -> 3 -> 2 then back to 3 and back to 1, works.

if 2 -> 4, then back to 2, works.

if 1 -> 3 -> 2 -> 4 then back to 2, tab bar is not showed. Wondering why. Any suggestions or some explanation of hidesBottomBarWhenPushed as it confuses me a lot

回答1:

As it's name suggest, hiddenBottomBarWhenPushed only hide bottom bar if needed, it will not unhide bottomBar. You can do this to get it works:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.tabBar.hidden = true/false
} 

or simply put self.tabBarController?.tabBar.hidden = true/false in prepareForSegue

But I would not recommend you to do so, as it would be weird if bottomBar suddenly popped out, user will thought they suddenly back to rootViewController while they are not.

Users should always know where they are in your app and how to get to their next destination.



回答2:

Add this implementation in ViewController you want to hide/show tabbar on pushed/popped. it will also work for all next pushed view controllers.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if wilmove {
        hidesBottomBarWhenPushed = true
    }
    wilmove = false
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if wilmove {
        hidesBottomBarWhenPushed = false
    }
    wilmove = false
}

var wilmove = false
override func willMove(toParentViewController parent: UIViewController?) {
    super.willMove(toParentViewController: parent)
    wilmove = true
    if !isViewLoaded {
        hidesBottomBarWhenPushed = true
    }
}


回答3:

Add hidesBottomBarWhenPushed property to destination view controller, and set to true.

Example with push VC with identifier:

    let storyboard = UIStoryboard(name: STORYBOARD_NAME, bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: VC_IDENTIFIER) as! YourViewController
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)