Adele
Adele

Reputation: 365

UITabBarController with UINavigationController

I have a UINavigationController inside a UITabBarController (tab 1). How can I make the tab bar disappear when I go into a second view (still in tab 1)? I can navigate back using the back button and the tab bar will reappear.

Upvotes: 1

Views: 1008

Answers (3)

Ashley D'Andrea
Ashley D'Andrea

Reputation: 5159

I like to use the view controller's init method to hide the bottom bar, among other things. Makes for nicer encapsulation of behavior.

(Note: The following is ARC-friendly code, hence no autorelease call or retain/release pairs.)

#pragma mark - UIViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    // We must handle this as it's the designated initializer for UIViewController.
    // Pay no attention to the params. We're going to override them anyway.
    return [self init];
}

#pragma mark - NSObject

- (id)init {
    // Why hello there, superclass designated initializer! How are you?
    if ((self = [super initWithNibName:@"YourNibNameHere" bundle:nil])) {
        // This is a perfect oppy to set up a number of things, such as ...

        // ... the title (since you're in a nav controller).
        self.navigationItem.title = @"Your Nav Title";

        // ... your bottom bar hiding (takes effect once pushed onto your nav controller).
        self.hidesBottomBarWhenPushed = YES;

        // ... and your tab bar item (since you're in a tab bar controller).
        [self setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"Item Title" image:[UIImage imageNamed:@"itemIcon.png"] tag:itemTag]];
    }
    return self;
}

Now all you have to do is alloc/init your view controller and call -pushViewController:animated:. No muss, no fuss.

When the VC is popped, your bottom bar will return. (Promise.)

Credit for this technique goes to Joe Conway of Big Nerd Ranch. (That's who I learned this terrific pattern from.)

As for using dot notation vs. not, well, that's an entirely different discussion. YMMV. ;)

Upvotes: 0

Nick Forge
Nick Forge

Reputation: 21464

In the viewController being pushed, put:

self.hidesBottomBarWhenPushed = YES;

in the -viewDidLoad method. It belongs in the 'child' VC, not the VC doing the pushing. You don't need to set it anywhere else.

Upvotes: 1

Ishu
Ishu

Reputation: 12787

self.hidesBottomBarWhenPushed=YES; put this line where you navigate (before push operation).

and self.hidesBottomBarWhenPushed=NO; in viewWillDisappear of same page from where you push other view.

It really works.

Upvotes: 4

Related Questions