hidesBottomBarWhenPushed = NO not working?

rottendevice picture rottendevice · Apr 12, 2011 · Viewed 22.8k times · Source

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.

Answer

Loz picture Loz · Apr 24, 2014

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.