【问题标题】:Hiding tabBar showing black bar in Ios7在 iOS7 中隐藏 tabBar 显示黑条
【发布时间】:2013-10-14 01:30:03
【问题描述】:

我正在使用此代码隐藏 TabBar:

self.tabBarController.tabBar.hidden=YES;

我将 tabBarController 隐藏在我的项目中。但它在 Ios7 的视图底部显示黑条。当我返回相同的视图时,它看起来不错。任何帮助将不胜感激。

【问题讨论】:

  • 你有布局限制或调整掩码集吗?可能是视图没有调整大小,所以它显示了控制器后面的开放空白
  • 请稍等。让我检查并告诉你
  • 我也有同样的问题?你有什么解决办法吗?
  • 我得到了一些运行良好的代码。你需要吗?
  • 查看一个简单的解决方案here

标签: ios objective-c swift xcode


【解决方案1】:

注意:它仅适用于 iOS6 和 7。

在 iOS 7 中,要扩展可点击区域并在隐藏的 UITabBar 的位置隐藏黑条,您应该为 UIViewController 启用“扩展边缘 - 在不透明条下”选项。

或者您可以通过编程方式设置此属性:

[self setExtendedLayoutIncludesOpaqueBars:YES]

这是隐藏或移动 iOS 6/7 的 TabBar 的代码示例:

UITabBarController *bar = [self tabBarController];
if ([self respondsToSelector:@selector(setExtendedLayoutIncludesOpaqueBars:)]) {
    //iOS 7 - hide by property
    NSLog(@"iOS 7");
    [self setExtendedLayoutIncludesOpaqueBars:YES];
    bar.tabBar.hidden = YES;
} else {
    //iOS 6 - move TabBar off screen
    NSLog(@"iOS 6");
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float height = screenRect.size.height;
    [self moveTabBarToPosition:height];
}

//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {

    self.tabBarController.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);

    for(UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
        } else {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
            view.backgroundColor = [UIColor blackColor];
        }
    }
}

将标签栏移出屏幕的功能来自this post

【讨论】:

  • 这在 iO8 中对我有用,我在正常的 TVC 中没有看到黑条,但是当我的 UISearchController 处于活动状态时,出现了混乱的黑条。尝试了几个小时来修复......我的 TableviewController 中的这个简单复选框修复了它。
  • 在 iOS8 上,使用“Under Opaque Bars”启用“Under Bottom Bars”,它可以工作。
  • 为我在 iOS8 上工作,勾选了“Extend Edges”下的所有选项!
  • 适用于我的 iOS 10
【解决方案2】:

下一个代码对我有用

- (void)showTabBar {
[self.tabBar setTranslucent:NO];
[self.tabBar setHidden:NO];
}

- (void)hideTabBar {
    [self.tabBar setTranslucent:YES];
    [self.tabBar setHidden:YES];
}

【讨论】:

  • ios 10 - 隐藏后黑色区域仍然存在
【解决方案3】:

试试这个:

- (BOOL)hidesBottomBarWhenPushed {
     return YES;
}

【讨论】:

    【解决方案4】:

    我在使用 UINavigationController 时遇到了一些麻烦:

    这是适用于 iOS 7 和 UINavigationControllers 的解决方案:

    头文件

    @interface UITabBarController (HideTabBar)
    - (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated;
    @end
    

    实施

    #import "UITabBarController+HideTabBar.h"
    
    @implementation UITabBarController (HideTabBar)
    
    - (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated {
      UIViewController *selectedViewController = self.selectedViewController;
      /**
      * If the selectedViewController is a UINavigationController, get the visibleViewController.
      * - setEdgesForExtendedLayout won't work with the UINavigationBarController itself.
      * - setExtendedLayoutIncludesOpaqueBars won't work with the UINavigationBarController itself.
      */
      if ([selectedViewController isKindOfClass:[UINavigationController class]])
        selectedViewController = ((UINavigationController *)selectedViewController).visibleViewController;
      __weak __typeof(self) weakSelf = self;
    
      void (^animations)(void) = ^{
          selectedViewController.edgesForExtendedLayout = UIRectEdgeAll;
          [selectedViewController setExtendedLayoutIncludesOpaqueBars:hide];
          weakSelf.tabBar.hidden = hide;
    
          /**
          * Just in case we have a navigationController, call layoutSubviews in order to resize the selectedViewController
          */
          [selectedViewController.navigationController.view layoutSubviews];
      };
    
      [UIView animateWithDuration:animated ? UINavigationControllerHideShowBarDuration : 0 animations:animations];
    }
    
    @end
    

    感谢 Vadim Trulyaev 指出 Extend Edges - Under Opaque Bars 标志!

    【讨论】:

      【解决方案5】:

      一行 Swift 3 答案。

      将以下内容放入您的 UIViewController 子类中:

          override var hidesBottomBarWhenPushed: Bool { get { return true }  set { self.hidesBottomBarWhenPushed = newValue }}
      

      【讨论】:

        【解决方案6】:

        在控制器中设置true属性hidesBottomBarWhenPushed隐藏。

        对于隐藏,你所有的控制器都放入prepare for segue

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            segue.destination.hidesBottomBarWhenPushed = true
        }
        

        【讨论】:

          【解决方案7】:
          To showTabbar:
          
          - (void)showTabBar:(UITabBarController *) tabbarcontroller
              {
                      //[UIView beginAnimations:nil context:NULL];
                      //[UIView setAnimationDuration:0.5];
                  for(UIView *view in tabbarcontroller.view.subviews)
                          {
          
          
                      if([view isKindOfClass:[UITabBar class]])
                              {
                          [view setFrame:CGRectMake(view.frame.origin.x, 521, view.frame.size.width, view.frame.size.height)];
          
                              }
                      else
                              {
                          [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 521)];
                              }
                          }
          
                      // [UIView commitAnimations];
              }
          
          To hide Tabbar:
           - (void)hideTabBar:(UITabBarController *) tabbarcontroller
              {
                      //[UIView beginAnimations:nil context:NULL];
                      //[UIView setAnimationDuration:0.5];
          
                  for(UIView *view in tabbarcontroller.view.subviews)
                          {
                      if([view isKindOfClass:[UITabBar class]])
                              {
                          [view setFrame:CGRectMake(view.frame.origin.x, 568, view.frame.size.width, view.frame.size.height)];
                              }
                      else
                              {
                          [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 568)];
                              }
                          }
          
                      //[UIView commitAnimations];
              }
          

          【讨论】:

          • 好吧,这隐藏了tabBar,这是肯定的,但是在iOS7中,tabbar曾经所在的空间是不可点击的......
          【解决方案8】:

          我花了很长时间来解决这个问题,试图在表格视图的底部放置一个响应按钮。我没有使用自动布局。我发现 iOS 6 和 7 之间的两个主要区别:

          1. 在 iOS7 上,当 tab bar 动画出来时,根视图控制器的视图不会延伸到 tab bar 所在的区域;它需要调整大小。

          2. 在 iOS7 上,只有 UITabBar 类型的视图需要在屏幕上进行动画关闭和显示。

          第 1 点的另一个问题是,如果在 iOS7 中,您将可见视图控制器主视图的子视图扩展到选项卡视图留下的空间,除非主视图被扩展,否则它将无法交互也是。考虑到这一点,我使用了以下代码:

          隐藏标签栏(将数学反转显示):

          [UIView animateWithDuration:kHideTabBarAnimationDuration animations:^{
              for(UIView *view in self.tabBarController.view.subviews)
              {
                  if([view isKindOfClass:[UITabBar class]])
                  {
                      [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y + view.frame.size.height, view.frame.size.width, view.frame.size.height)];
                  }
                  else
                  {
                      if (![MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
                      {
                          [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height + self.tabBarController.tabBar.frame.size.height)];
                      }
                  }
              }
          } completion:nil];
          

          隐藏标签栏时调整主视图:

          // Expand view into the tab bar space
          if ([MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
          {
              CGRect frame = self.view.frame;
              self.view.frame = CGRectMake(frame.origin.x,
                                           frame.origin.y,
                                           frame.size.width,
                                           frame.size.height + tabBarHeight);
          }
          

          调整显示标签栏时的主视图:

          [UIView animateWithDuration:kHideTabBarAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
              // Create space for the tab bar            
              if ([MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
              {
                  CGRect frame = self.view.frame;
                  self.view.frame = CGRectMake(frame.origin.x,
                                               frame.origin.y,
                                               frame.size.width,
                                               frame.size.height - tabBarHeight);
              }
          } completion:nil];
          

          请注意,当隐藏标签栏时,我不会为主视图展开动画,这看起来很自然,因为展开发生在标签栏后面。

          另请注意

          在 iOS 7 中,如果在标签栏隐藏的情况下从纵向旋转到横向,黑框会重新出现。我通过在旋转动画之前将标签栏动画化回到屏幕上解决了这个问题(这对于我正在处理的工作来说已经足够了)。

          【讨论】:

            【解决方案9】:

            基于@Vadim Trulyaev 的解决方案,我创建了一个简单的用法:

            UITabBarController+HideTabBar.h

            @interface UITabBarController (Additions)
            - (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass;
            @end
            

            UITabBarController+HideTabBar.m

            #import "UITabBarController+HideTabBar.h"
            
            @implementation UITabBarController (HideTabBar)
            
            - (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass{
                if ([myClass respondsToSelector:@selector(setExtendedLayoutIncludesOpaqueBars:)]) {
                    //iOS 7 - hide by property
                    NSLog(@"iOS 7");
                    [myClass setExtendedLayoutIncludesOpaqueBars:hidden];
                    self.tabBar.hidden = hidden;
                } else {
                    //iOS 6 - move TabBar off screen
                    NSLog(@"iOS 6");
                    CGRect screenRect = [[UIScreen mainScreen] bounds];
                    float height = screenRect.size.height;
                    if(hidden){
                        [self moveTabBarToPosition:height];
                    }else{
                        [self moveTabBarToPosition:height - self.tabBar.frame.size.height];
                    }
                }
            }
            
            //Moving the tab bar and its subviews offscreen so that top is at position y
            -(void)moveTabBarToPosition:(int)y {
                self.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
            
                for(UIView *view in self.view.subviews) {
                    if ([view isKindOfClass:[UITabBar class]]) {
                        [view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
                    } else {
                        NSLog(@"%f",view.frame.size.height);
                        [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
                        NSLog(@"%f",view.frame.size.height);
                        view.backgroundColor = [UIColor blackColor];
                    }
                }
            }
            
            @end
            

            使用方法:

            [[self tabBarController] setTabBarHidden:NO myClass:self];
            

            但是,在 iOS6 中我有一些问题,当我第一次去 ViewController1 隐藏标签栏的地方时一切正常,但是如果我去一个显示标签栏的 ViewController2 并返回 ViewController1标签栏必须隐藏,黑色空间出现。任何人都可以帮助我吗?!

            谢谢!

            【讨论】:

              【解决方案10】:

              除了其他出色的建议之外,以下建议可能会对某人有所帮助。尝试将标签栏设置为隐藏在 awakeFromNib 中,而不是在生命周期的后期。我发现隐藏的标签栏在 segue 上闪烁黑色,这为我修复了它。

              - (void)awakeFromNib { [super awakeFromNib]; self.tabBarController.tabBar.hidden = YES; }

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-05-24
                • 2021-06-04
                • 1970-01-01
                • 2021-10-18
                • 2018-06-05
                相关资源
                最近更新 更多