隐藏后显示标签栏

时间:2010-08-23 04:11:58

标签: iphone controller uitabbarcontroller

隐藏标签栏后有没有办法显示标签栏?

有一个tabbar-nav结构。对于其中一个选项卡,我需要隐藏其第二级和第三级视图的选项卡栏。但与此同时,我需要展示其第一和第四视图。

我认为来自Elements的示例代码并不适用于此。

4 个答案:

答案 0 :(得分:4)

我找到了一个很好的实用解决方案来解决这个问题 - 让UITabBarController的视图比它需要的大,这样实际的UITabBar会被屏幕剪掉。

假设标签栏视图通常填充其超级视图,这种事情应该起作用:

CGRect frame = self.tabBarController.view.superview.frame;
if (isHidden)
{
    CGFloat offset = self.tabBarController.tabBar.frame.size.height;
    frame.size.height += offset;
}
self.tabBarController.view.frame = frame;

标签栏仍然显示,但它位于屏幕底部,因此似乎已隐藏。

如果它导致额外的削波,它可能会对性能产生影响,但到目前为止,它似乎有效。

答案 1 :(得分:1)

您需要实现委托方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController2 shouldSelectViewController:(UIViewController *)viewController

在里面你可以检查选择了哪个索引并显示标签栏

if([[tabBarController.viewControllers objectAtIndex:0] isEqual:viewController])// it is first tab
{
      tabBarController.tabBar.hidden = FALSE;
}

答案 2 :(得分:1)

推入导航堆栈的UIViewControllers可以执行以下操作:

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO; // Or YES as desired.
}

编辑:在下面添加了额外的代码来处理框架。不要以为我特别推荐这个想法,因为它依赖于UITabBarController的内部默认视图结构。

在UITabBarController上定义以下类别:

@interface UITabBarController (Extras)
- (void)showTabBar:(BOOL)show;
@end

@implementation UITabBarController (Extras)
- (void)showTabBar:(BOOL)show {
    UITabBar* tabBar = self.tabBar;
    if (show != tabBar.hidden)
        return;
    // This relies on the fact that the content view is the first subview
    // in a UITabBarController's normal view, and so is fragile in the face
    // of updates to UIKit.
    UIView* subview = [self.view.subviews objectAtIndex:0];
    CGRect frame = subview.frame;
    if (show) {
        frame.size.height -= tabBar.frame.size.height;
    } else {
        frame.size.height += tabBar.frame.size.height;
    }
    subview.frame = frame;
    tabBar.hidden = !show;
}
@end

然后,不要使用我最初建议的tabBar.hidden更改,请执行以下操作:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBarController showTabBar:NO];
}

显然确保实现包含了类别定义,以便知道'showTabBar'。

答案 3 :(得分:0)

我知道这是一个旧帖子,但我认为下面的代码有助于隐藏你不想要它的视图控制器上的标签栏,还有一个额外的好处就是当你从那个视图控制器回来时自动读取标签栏

UIViewController *hideTabbarViewController = [[UIViewController alloc] init];  
hideTabbarViewController.hidesBottomBarWhenPushed = YES;  
[[self navigationController] hideTabbarViewController animated:YES]; 
相关问题