简体   繁体   中英

iOS- How to Hide/Show UITabBarController's tab bar with animation?

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?

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;
    }];
}

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;
        }];
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM