views:

192

answers:

2

Hi folks,

Is there any way to show a tab bar after it has been hidden? Got a tabbar-nav structure. For one of the tabs, I need to hide the tab bar for its 2nd and 3rd level view. But at the same time I will need to show its 1st and 4th view.

The sample code from Elements isn't really applicable here I think.

Any help is greatly appreciated.

A: 

you need to implement delegate method

- (BOOL)tabBarController:(UITabBarController *)tabBarController2 shouldSelectViewController:(UIViewController *)viewController

inside that you can check which index is selected and show the tabbar

if([tabBarController.viewControllers objectAtIndex:0] isEqual:viewController])// it is first tab
{
      tabBarControllertabBar.hidden = FALSE;
}
mihirpmehta
Hi mihirpmehta,Not exactly the situation I am in. For my situation its a tabbar-nav structure. I hope to show the tab bar for the 1st and 4th level. But hide it for the 2nd and 3rd view.The one you described is after clicking the tab bar items?Wonder if I can do something likedelegate.tabbarcontroller.tabbar.hidden = falsedelegate.viewController.hidesBottomBarWhenPushed = NO doesn't seem to return the tabbar back to view.setting self.hidesBottomBarWhenPushed = NO at viewDidLoad is not working also
ngzhongcai
You're correct that hidesBottomBarWhenPushed is useless for this because as documented "the bottom bar remains hidden until the view controller is popped from the stack."
imaginaryboy
A: 

The UIViewControllers that are pushed onto the navigation stack can do the something like the following:

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO; // Or YES as desired.
}

EDIT: Added additional code below to deal with the frame. Don't think I particular recommend this idea since it relies on the internal default view structure of a UITabBarController.

Define the following category on UITabBarController:

@interface UITabBarController (Extras)
- (void)showTabBar:(BOOL)show;
@end

@implementation UITabBarController (Extras)
- (void)showTabBar:(BOOL)show {
    UITabBar* tabBar = self.tabBar;
    if (show != tabBar.hidden)
        return;
    // This relies on the fact that the content view is the first subview
    // in a UITabBarController's normal view, and so is fragile in the face
    // of updates to UIKit.
    UIView* subview = [self.view.subviews objectAtIndex:0];
    CGRect frame = subview.frame;
    if (show) {
        frame.size.height -= tabBar.frame.size.height;
    } else {
        frame.size.height += tabBar.frame.size.height;
    }
    subview.frame = frame;
    tabBar.hidden = !show;
}
@end

Then, instead of using the tabBar.hidden change I originally suggested, do the following:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBarController showTabBar:NO];
}

Obviously making sure that the implementation has included the category definition so that 'showTabBar' is known.

imaginaryboy
Tried the above... what happens is the tabbar gets hidden, but in place there's a white ugly empty strip where the tabbar is 'hidden'
ngzhongcai
Ah, yes, makes sense, and of course I didn't notice it because in the quick sample I tried my views already were white.It seems as though you might actually have to affect the frame of the whatever view is in the main display area to take up the space left by the hidden tabBar. That seems a bit of a hack to try to get it to cooperate though.
imaginaryboy