2

I want

view controller 1: tab bar is showed

view controller 2: tab bar is showed

view controller 3: tab bar is not showed.

I wrote

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

// prepareForSegue in view controller 3,
let upcoming = segue.destinationViewController as! viewcontroller2
self.hidesBottomBarWhenPushed = true

When I go to view controller 3 from view controller 1, tab bar is not showed. Then, I go to view controller 2 from view controller 3, tab bar is showed. But when I tap back in view controller 2, tab bar is showed in view controller 3. self.hidesBottomBarWhenPushed = true does not make sense to me. But, I couldn't figure what I should do to fix that. Any suggestions? Thanks.

enter image description here

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52

4 Answers4

13

The way to hide the tab bar is - In the place where you are pushing the next view controller do that:

self.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(controllerToPush, animated: true) 
self.hidesBottomBarWhenPushed = false

This will make sure the tab bar is hidden for the pushed view and when you pop back the bar will show again. No back button logic, no viewDidLoad or similar, nothing else. This should be enough.

The same should work for objective-c

Now ( 21/02/2018 ) you can also check the option in the Storyboard for each controller that you do not want to show the bottom bar. This will help clean up the code and you do not need to set anything in the controller that you are pushing from.enter image description here

Stan
  • 1,513
  • 20
  • 27
  • Property should be set on the controller that is about to be pushed as follows controllerToPush.hidesBottomBarWhenPushed = true self.navigationController?.pushViewController(controllerToPush, animated: true) – btomtom5 Mar 28 '19 at 06:28
  • @btomtom5 My example actually works perfectly, this was intentionally placed in the controller that is pushing – Stan Mar 19 '20 at 09:21
  • Hmmm. But the view controller that is on the top of the nav controller stack is the one that determines whether the bottom bar is shown or not. The viewcontroller that is adding the child vc to the navcontroller shouldn't have any effect on whether the bottom bar is shown or not. – btomtom5 Mar 21 '20 at 03:22
8

Edit: That solved the problem.

It makes sense that the tab bar is appearing because when going clicking back from VC2 to VC3, nothing is telling VC3 to hide its Tab Bar.

I think you have 2 solutions here (but haven't tested any):

  1. You can try doing something like this guy did. He added the hidesBottomBarWhenPushed logic in BackButtonPressed Handler.
  2. In VC3, do self.tabBarController?.tabBar.hidden = true in ViewDidLoad or viewWillAppear
Community
  • 1
  • 1
Guy Daher
  • 5,526
  • 5
  • 42
  • 67
3

@stan has almost the right answer. As he mentioned, you want to set to set hidesBottomBarWhenPushed = true if you want the bottom bar to be hidden. However, you should set it on the controller to be pushed as follows.

controllerToPush.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(controllerToPush, animated: true)
btomtom5
  • 842
  • 8
  • 16
0

Set hidesBottomBarWhenPushed = true in the controller that you want to hide.

For hide all controllers put into prepare for segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.hidesBottomBarWhenPushed = true
}
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62