Iphone:可以隐藏TabBar吗?(iOS 8之前)

Ste*_*eve 34 iphone objective-c uitabbarcontroller ios

我有一个使用a UITabBarController在模式之间切换的应用程序.在某种模式下,我想隐藏标签栏,直到该模式的步骤完成.请注意,我没有使用导航控制器,所以我无法使用setHidesBottomBarWhenPushed导航控制器上的方法来隐藏标签栏.

在iOS 8之前,当我尝试使用以下命令隐藏tarbar时:

self.tabBarController.tabBar.hidden = YES
Run Code Online (Sandbox Code Playgroud)

标签栏消失,但它在屏幕底部留下了一个50像素的空白区域,标签栏曾经是.我似乎无法弄清楚如何填补该区域.用户界面中该区域中的任何内容都被剪切,无法看到.

任何想法,如果这是可能的?我真的很想远离导航控制器.

bsh*_*ley 37

这是我的代码:

这是当然的,具有出渣行径在控制器的视图层次结构.它可以改变/破坏.这使用已定义的API,因此Apple不会关心,但他们也不会在意破坏您的代码.

- (void)hideTabBar {
  UITabBar *tabBar = self.tabBarController.tabBar;
  UIView *parent = tabBar.superview; // UILayoutContainerView
  UIView *content = [parent.subviews objectAtIndex:0];  // UITransitionView
  UIView *window = parent.superview;

  [UIView animateWithDuration:0.5
                   animations:^{
                     CGRect tabFrame = tabBar.frame;
                     tabFrame.origin.y = CGRectGetMaxY(window.bounds);
                     tabBar.frame = tabFrame;
                     content.frame = window.bounds;
                   }];

  // 1
}

- (void)showTabBar {
  UITabBar *tabBar = self.tabBarController.tabBar;
  UIView *parent = tabBar.superview; // UILayoutContainerView
  UIView *content = [parent.subviews objectAtIndex:0];  // UITransitionView
  UIView *window = parent.superview;

  [UIView animateWithDuration:0.5
                   animations:^{
                     CGRect tabFrame = tabBar.frame;
                     tabFrame.origin.y = CGRectGetMaxY(window.bounds) - CGRectGetHeight(tabBar.frame);
                     tabBar.frame = tabFrame;

                     CGRect contentFrame = content.frame;
                     contentFrame.size.height -= tabFrame.size.height;
                   }];

  // 2
}
Run Code Online (Sandbox Code Playgroud)

编辑:一位匿名用户建议为7.0添加以下内容(我没有对此进行测试,也无法说明它是变通方法还是理想的实现方式):

// 1. To Hide the black line in IOS7 only, this extra bit is required
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    [self.tabBarController.tabBar setTranslucent:YES];
}  

// 2. For IOS 7 only
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    [self.tabBarController.tabBar setTranslucent:NO];
}
Run Code Online (Sandbox Code Playgroud)

编辑:在8.x中完全未经测试,可能缺少某些布局.

  • 这很棒!我们完成此操作的旧方法导致屏幕上没有响应的部分,其中标签栏曾经是,但此方法没有.如果您同时隐藏导航栏,我建议将动画持续时间更改为"UINavigationControllerHideShowBarDuration". (4认同)
  • 实际上,事实证明这个方法会导致我的子视图帧出现问题,所以我不得不使用`hidesBottomBarWhenPushed`属性. (3认同)

小智 9

像史蒂夫一样,我还没有找到一个干净的方法来做到这一点(尽管Apple Photopicker做了类似的事情).这是我做的:

 if (systemAction)
  {
    // Reveal tab bar back
    CGRect bounds = [[UIScreen mainScreen] bounds];
    CGRect tabBarFrame = self.tabBarController.tabBar.frame;
    self.tabBarController.view.frame = CGRectMake(0,0,bounds.size.width,bounds.size.height);
    self.toolBar.hidden = YES;
    systemAction = NO;
  }
  else
  {
    //hide tab bar
    CGRect bounds = [[UIScreen mainScreen] bounds];
    CGRect tabBarFrame = self.tabBarController.tabBar.frame;
    CGRect navigationBarFrame = self.navigationController.navigationBar.frame;
    self.tabBarController.view.frame = CGRectMake(0,0,bounds.size.width,bounds.size.height+tabBarFrame.size.height);
    self.toolBar.hidden = NO;
    CGRect frame = self.toolBar.frame;
    frame.origin.y = bounds.size.height - frame.size.height - navigationBarFrame.size.height;
    self.toolBar.frame = frame;
    systemAction = YES;
  }
Run Code Online (Sandbox Code Playgroud)

它正在做的是推动视图,以便我可以显示一个工具栏(而不是隐藏它).显然,这仅适用于tabbar +导航控制器的"根视图".对于任何后续视图,您可以在要推送的视图控制器上设置"hidesBottomBarWhenPushed".


GAl*_*lan 8

我尝试了上面的一些解决方案,但在iOS 8中没有任何乐趣.我发现viewWillAppear中的设置对我来说有以下作用.应该在iOS 7中工作,因为然后引入了extendedLayoutIncludesOpaqueBars.

    self.extendedLayoutIncludesOpaqueBars = true
    self.tabBarController?.tabBar.isHidden = true
    self.tabBarController?.tabBar.isOpaque = true
Run Code Online (Sandbox Code Playgroud)

如果您需要在viewWillDisappear中使用以下内容时再次打开tabBars.

    self.tabBarController?.tabBar.isHidden = false
    self.tabBarController?.tabBar.isOpaque = false
Run Code Online (Sandbox Code Playgroud)

我使用它来允许从过渡返回以保持TabBar隐藏.没有在按钮动作中使用它,但如果像我一样,你发现现在没有任何东西可以工作,这可能是可编程解决方案的基础.


Mys*_*ral 0

您是否在子视图上设置了 autoResizingMask ?

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Run Code Online (Sandbox Code Playgroud)

类似的东西应该可以解决问题,并允许位于堆栈顶部的视图重新调整大小。