hide / show tab bar when push / back. swift
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)
Here's my two cents. Swift 3/4/5:
Approach 1: (Recommended)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourSegueIdentifier" {
let destinationController = segue.destinationViewController as! YourViewController
destinationController.hidesBottomBarWhenPushed = true // Does all the hide/show work.
}
}
Approach 2:
override func viewWillAppear(_ animated: Bool) { // As soon as vc appears
super.viewWillAppear(true)
self.tabBarController?.tabBar.isHidden = false
}
override func viewWillDisappear(_ animated: Bool) { // As soon as vc disappears
super.viewWillDisappear(true)
self.tabBarController?.tabBar.isHidden = true
}
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
}
}
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.