3

I have a question about iOS's UITabBarController's tab bar.

I'm using a UITabBarController to display a few views, but as I want the views to be displayed with as large a screen as possible. Is it possible to hide the tab bar so that it normally doesn't show, until the user touches the screen, then the tab bar will (with animation) show up at the bottom. Then, after a few seconds, if nothing is done, then the tab bar will again go away, so that the view going back to be full screen again?

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
JusmanX
  • 751
  • 1
  • 7
  • 5

3 Answers3

5

This is how you show it

- (void)showTabBar:(UITabBarController *)tabbarcontroller
{
    tabbarcontroller.tabBar.hidden = NO;
    [UIView animateWithDuration:kAnimationInterval animations:^{
        for (UIView *view in tabbarcontroller.view.subviews) {
            if ([view isKindOfClass:[UITabBar class]]) {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y-49.f, view.frame.size.width, view.frame.size.height)];
            }
            else {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height-49.f)];
            }
        }
    } completion:^(BOOL finished) {
        //do smth after animation finishes
    }];
}

... and this is how you hide it

- (void)hideTabBar:(UITabBarController *)tabbarcontroller
{
    [UIView animateWithDuration:kAnimationInterval animations:^{
        for (UIView *view in tabbarcontroller.view.subviews) {
            if ([view isKindOfClass:[UITabBar class]]) {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y+49.f, view.frame.size.width, view.frame.size.height)];
            }
            else {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height+49.f)];
            }
        }
    } completion:^(BOOL finished) {
        //do smth after animation finishes
        tabbarcontroller.tabBar.hidden = YES;
    }];
}
Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91
  • Thanks, this answer helped me. :) I also set the tab bar `hidden` property to `YES` in the completion block. It's useful for determining whether or not you need to show the tab bar when a view appears. – James Oct 12 '12 at 12:40
  • Your original code was already very helpful, but since you decided to update it, for `-showTabBar:` the `hidden` property should be set to `NO` before the animation block (rather than in the completion block). Otherwise, the effect will look very strange. – James Oct 13 '12 at 09:53
2

With the accepted answer, on iOS 7 when you hide the tab bar and you show it again the size is wrong. This code gives a better result:

- (void) toggleTabBar: (UITabBar *)tabBar view: (UIView*) view {

    tabBar.hidden = NO;

    [UIView animateWithDuration:0.5 animations:^{
            if (hiddenTabBar) {
                tabBar.center = CGPointMake(tabBar.center.x, self.view.window.bounds.size.height-tabBar.bounds.size.height/2);
            }
            else {
                tabBar.center = CGPointMake(tabBar.center.x, self.view.window.bounds.size.height+tabBar.bounds.size.height);
            }

        } completion:^(BOOL finished) {
            hiddenTabBar = !hiddenTabBar;
            tabBar.hidden = hiddenTabBar;
        }];
}
José Manuel Sánchez
  • 5,215
  • 2
  • 31
  • 24
1

Don't think that will work on Apple's UIGuidelines. The views you're using are drawn above the the tab bar, so if you fade it away, nothing will be there.

You could possibly make a small view with buttons in place of the tab bar that does what you want.

Hobbes the Tige
  • 3,753
  • 2
  • 22
  • 21