iOS- How to Hide/Show UITabBarController's tab

2020-02-09 19:12发布

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?

3条回答
Juvenile、少年°
2楼-- · 2020-02-09 19:57

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;
    }];
}
查看更多
仙女界的扛把子
3楼-- · 2020-02-09 20:03

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.

查看更多
够拽才男人
4楼-- · 2020-02-09 20:13

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;
        }];
}
查看更多
登录 后发表回答