Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hidesBottomBarWhenPushed = NO not working?

I have a UITabBar in my app, which I'm hiding on the first UIViewController in the first tab by putting this line in the AppDelegate:

// ... in MyAppDelegate.m
firstViewController.hidesBottomBarWhenPushed = YES;

In the firstViewController, the user can push a UIButton that pushes a new UIViewController in the same tab. I'd like for the UITabBar to be visible again when this happens. I'm trying to make it come back like this:

//... in firstViewController.m

secondViewController = [[SecondViewController alloc] init];
secondViewController.hidesBottomBarWhenPushed = NO;
[[self navigationController] pushViewController:secondViewController animated:YES];

Unfortunately, does not bring back the UITabBar. It remains hidden.

How do I properly bring bar the UITabBar after hiding it?

Thanks in advance.

like image 502
rottendevice Avatar asked Apr 12 '11 20:04

rottendevice


2 Answers

This is an issue that has bugged me for a while, and I only just found a solution that works. The hidesBottomBarWhenPushed property is a very strange beast, and works in, to my mind, a counter-intuitive way.

The problem with it is that when you push a new view controller (or pop back) the navigationController will ask all view controllers (from top to bottom) if they want to hide the bottom bar, and if any of them say YES the tabbar will be hidden, which is why the tabbar remains hidden despite setting NO to hiding on the new view controller.

Here is my solution - override the hidesBottomBarWhenPushed getter in the view controller that you wish to not have a tabbar, and check if it is at the top of the stack:

Objective-C

- (BOOL) hidesBottomBarWhenPushed
{
    return (self.navigationController.topViewController == self);
}

Swift (not so obvious, hence snippet)

override var hidesBottomBarWhenPushed: Bool {
    get {
        return navigationController?.topViewController == self
    }
    set {
        super.hidesBottomBarWhenPushed = newValue
    }
}

This nicely encapsulates the hide/show logic in one place, so you dont have to think about it outside of the viewcontroller that does the hiding.

like image 138
Loz Avatar answered Oct 12 '22 00:10

Loz


This is what the documentation for hidesBottomBarWhenPushed says (emphasis added):

If YES, the bottom bar remains hidden until the view controller is popped from the stack.

So it looks like the behavior you're seeing is just what the documentation says will happen. You start by pushing a view controller onto the stack which has hidesBottomBarWhenPushed = YES. At that point, pushing other view controllers onto the stack won't change the hiddenness of the bottom bar. As long as that first view controller is on the stack, the bottom bar will remain hidden.

So I think you'll have to come up with a different way of accomplishing your UI goal. One option would be to present the first view controller as a modal view controller over the tab bar controller's view. Then, when you want to go to the second view controller just dismiss the first one and voila. The only visual difference will be the transition animation.

There are surely other options too, but that just came first to my mind.

Good luck!

like image 30
CharlieMezak Avatar answered Oct 12 '22 00:10

CharlieMezak