eLwoodianer
eLwoodianer

Reputation: 674

Hide/Show TabBar in ViewController loops

Situation:

I have a few ViewControllers (all with a NavigationBar) embedded in a TabBarController. I have one specific ViewController (VC1) where I don't want to show the TabBar. From there you can go to another specific ViewController (VC2), where the TabBar needs to be shown again.

My solution:

VC1

self.hidesBottomBarWhenPushed is set to true by default

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {      
    self.hidesBottomBarWhenPushed = false
}

override func viewWillDisappear(animated: Bool) {
    self.hidesBottomBarWhenPushed = true
}

VC2

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {      
    self.hidesBottomBarWhenPushed = true
}

override func viewWillDisappear(animated: Bool) {
    self.hidesBottomBarWhenPushed = false
}

So far so good. This seems to be working, but you can push another VC1 from VC2 (Same controller with different content) and of course push another VC2 from VC1 again and so on.

The Problem:

  1. As soon as VC2 is pushed twice from a VC1 the TabBar is always hidden.
  2. When you hit the back button to go from a VC1 back to a VC2 the TabBar is always hidden.

enter image description here

Upvotes: 0

Views: 280

Answers (4)

luk2302
luk2302

Reputation: 57184

Do not put the logic in viewWillDisappear or prepareForSegue since you do not know what kind of behavior the view controller that is about to be presented wants. Put the logic inside viewWillAppear instead.

Let every ViewController handle its own desired behavior and do not try to anticipate what the destination wants. Especially because you do not always know what the reason for viewWillDisappear or prepareForSegue is - therefore you cannot react accordingly.

Upvotes: 1

Jogendra.Com
Jogendra.Com

Reputation: 6454

Try these code in viewWillApear for hide or unhide , it'll work fine .

For VC1 : - In this you want always hide then add this code

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

For VC2 : - In this you want always show then add this code

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

enter image description here Try this code , Its working fine . I also tried this code in sample project.

Upvotes: 0

Savchenko Dmitry
Savchenko Dmitry

Reputation: 360

Customize only VC1

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

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

It is simpler architecture

Upvotes: 2

Eugene Zaychenko
Eugene Zaychenko

Reputation: 319

Try change property in needed VC:

self.navigationController.toolbarHidden = YES;

Upvotes: 0

Related Questions