iOS-如何使用动画隐藏/显示UITabBarController的标签栏?

时间:2011-04-04 19:52:11

标签: ios tabs uitabbarcontroller tabbar

我对iOS的UITabBarController标签栏有疑问。

我正在使用UITabBarController来显示一些视图,但我希望视图能够以尽可能大的屏幕显示。是否可以隐藏标签栏以使其通常不显示,直到用户触摸屏幕,然后标签栏将(带动画)显示在底部。然后,几秒钟后,如果没有任何操作,那么标签栏将再次消失,以便视图再次返回全屏?

3 个答案:

答案 0 :(得分:5)

这是你展示它的方式

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

......这就是你隐藏它的方式

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

答案 1 :(得分:2)

根据接受的答案,在iOS 7上隐藏标签栏并再次显示时尺寸错误。此代码提供了更好的结果:

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

答案 2 :(得分:1)

不要认为这会对Apple的UIGuidelines起作用。你正在使用的视图被绘制在标签栏上方,所以如果你淡出它,那里什么都没有。

您可以用按钮代替标签栏来制作一个小视图,它可以满足您的需要。