Reputation: 674
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:
TabBar
is always hidden.TabBar
is always hidden.Upvotes: 0
Views: 280
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
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
}
Try this code , Its working fine . I also tried this code in sample project.
Upvotes: 0
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
Reputation: 319
Try change property in needed VC:
self.navigationController.toolbarHidden = YES;
Upvotes: 0