【问题标题】:iOS- How to Hide/Show UITabBarController's tab bar with animation?iOS-如何隐藏/显示带有动画的 UITabBarController 标签栏?
【发布时间】:2011-07-29 10:51:16
【问题描述】:

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

我使用UITabBarController 来显示一些视图,但我希望这些视图以尽可能大的屏幕显示。是否可以隐藏标签栏以使其通常不显示,直到用户触摸屏幕,然后标签栏将(带有动画)显示在底部。然后,几秒钟后,如果什么都不做,标签栏又会消失,让视图又回到全屏状态?

【问题讨论】:

    标签: ios tabs uitabbarcontroller tabbar


    【解决方案1】:

    这就是你展示它的方式

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

    【讨论】:

    • 谢谢,这个答案帮助了我。 :) 我还在完成块中将标签栏hidden 属性设置为YES。这对于确定是否需要在视图出现时显示标签栏很有用。
    • 您的原始代码已经非常有帮助,但是由于您决定更新它,对于-showTabBar:hidden 属性应该在动画块之前设置为NO(而不是在完成堵塞)。不然效果会很奇怪。
    【解决方案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;
            }];
    }
    

    【讨论】:

      【解决方案3】:

      不要认为这适用于 Apple 的 UIGuidelines。您正在使用的视图绘制在选项卡栏上方,因此如果您将其淡出,则不会出现任何内容。

      您可以使用按钮代替选项卡栏创建一个小视图,以执行您想要的操作。

      【讨论】:

        猜你喜欢
        • 2014-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-11
        • 1970-01-01
        相关资源
        最近更新 更多