Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using hidesBottomBarWhenPushed, i want the tab bar to reappear when i push another view

I have a navigation controller. For one of the views i want to hide the bottom tab bar, so it gets the max possible screen real estate. To do this, i have:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES; // To hide the tab bar
    }
    return self;
}

But for the next view that i push on the stack, i want the tab bar to reappear. Is there a way to do this?

like image 375
Chris Avatar asked May 24 '11 22:05

Chris


3 Answers

I'm have solved this problem like that:

Almost all my ViewControllers are children of BaseViewController.

So, example:

class BaseVC: UIViewController {
    final override var hidesBottomBarWhenPushed: Bool {
        get {
            if navigationController?.viewControllers.last == self {
                return prefersBottomBarHidden ?? super.hidesBottomBarWhenPushed
            } else {
                return false
            }
        } set {
            super.hidesBottomBarWhenPushed = newValue
        }
   }
   private(set) var prefersBottomBarHidden: Bool?
}

Just override variable "prefersBottomBarHidden" in ViewController where BottomBar should be hidden:

override var prefersBottomBarHidden: Bool? { return true }
like image 198
Dmytro Shvetsov Avatar answered Nov 13 '22 01:11

Dmytro Shvetsov


As of iOS5, there's a very easy means of accomplishing this. It's essentially the same method as Deepak, but there aren't any artifacts with the animation - everything looks as expected.

On init, set

self.hidesBottomBarWhenPushed = YES;

just as you have above. When it's time to push the new controller on the stack, it's as simple as:

self.hidesBottomBarWhenPushed = NO;

UIViewController *controller = [[[BBListingController alloc] init] autorelease];
[self.navigationController pushViewController:controller];

self.hidesBottomBarWhenPushed = YES;

It's important to reset the value to YES after the controller has been pushed in order to re-hide the bar when the user taps the Back button and the view comes back into view.

like image 13
Andrew Vilcsak Avatar answered Nov 13 '22 03:11

Andrew Vilcsak


It's been a while since this question was asked, but none of these answers address using Storyboard segues. It turns out to be pretty easy:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "MyViewControllerIdentifier" {

        // Hide the tabbar during this segue
        hidesBottomBarWhenPushed = true

        // Restore the tabbar when it's popped in the future
        DispatchQueue.main.async { self.hidesBottomBarWhenPushed = false }

    }
}
like image 5
Troy Avatar answered Nov 13 '22 03:11

Troy