【问题标题】:UITabBarController subviews disappear when hidesBottomBarWhenPushed is set to YES only on iOS7仅在 iOS7 上将 hidesBottomBarWhenPushed 设置为 YES 时,UITabBarController 子视图消失
【发布时间】:2013-11-19 01:58:02
【问题描述】:

我创建了一个 UITabBarController 的子类,它在选项卡顶部添加了一个自定义 UIView 以替换 UITabBarItem 上的默认标记。我在自定义 UITabBarController 的 viewDidLoad 上添加了这些自定义徽章视图。

[self.view addSubview:_tabBarItem1BadgeView];

我的自定义 tabBarBadgeView 上的 drawRect 如下所示:

- (void)drawRect:(CGRect)rect{

  CGContextRef ctx = UIGraphicsGetCurrentContext();
  // Badge
  CGSize size = self.frame.size;
  CGSize badgeSize = [self sizeThatFits:size];
  badgeSize.height = fminf(badgeSize.height, size.height);
  CGFloat x = roundf((size.width - badgeSize.width) / 2.0f);    
  CGRect badgeRect = CGRectMake(x, roundf((size.height - badgeSize.height) / 2.0f), badgeSize.width, badgeSize.height);
  CGContextAddEllipseInRect(ctx, rect);
  CGContextSetFillColor(ctx, CGColorGetComponents([_badgeColor CGColor]));
  CGContextFillPath(ctx);

  [_textLabel drawTextInRect:badgeRect];
}

效果很好。我可以在我添加它的位置看到一个 badgeView。如果我切换选项卡,则不会受到任何影响。

tabControllers 的所有视图控制器都是 UINavigationControllers。我有一个用例,其中一个选项卡的 UINavigationController 最顶层的 UIViewController 不应该显示 tabBar,所以我自然设置了

controller.hidesBottomBarWhenPushed = YES

在推入 navigationController 堆栈之前。这成功地抑制了 tabBar,但我的自定义 UIViews 继续存在。为了解决这个问题,我将我的自定义 UITabBarController 设为 UINavigationControllerDelegate。这允许我在导航推送和弹出时手动隐藏和显示这些自定义 UIView。

我这样做是:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController
                animated:(BOOL)animated

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated

效果很好....仅在 iOS6 中。

在 iOS7 上,自定义 UITabBarController 从将 hidesBottomBarWhenPushed 设置为 YES 的 UIViewController 弹出后将不会显示自定义 UIView。如果 hidesBottomBarWhenPushed 设置为 NO,则 UIViews 会继续出现。

事实上,我完全删除了 UINavigationControllerDelegate,奇怪的是,当我在 UINavigationController 上向下钻取堆栈时(使用 hidesBottomBarWhenPushed=YES),tabBar 是隐藏的,但我添加的自定义 UIViews 仍然存在(这是我所期望的)。但是当我从那里弹出时(这是最奇怪的部分),我回到选项卡的顶级控制器(应该显示 tabBar),并且 tabBar 是可见的(预期的),但是自定义 UIViews 消失了。点击返回,它们出现,按返回,它们消失。

而且这种行为只发生在 iOS7 上。在显示带有 hidesBottomBarWhenPushed = YES 的 UIViewController 然后返回带有 hidesBottomBarWhenPushed = NO 的 UIViewController 之后,UITabBar 是否发生了不同的事情?

【问题讨论】:

    标签: ios ios7 uitabbarcontroller


    【解决方案1】:

    每当 UITabBarController 将自己的视图重新添加到其层次结构时,您的自定义徽章视图就会隐藏在 UITabBarController 自己的视图后面。

    要使您的自定义徽章视图始终位于顶部,您可以覆盖自定义徽章视图上的 drawRect 并调用 [self.superview bringSubviewToFront:self];

    - (void)drawRect:(CGRect)rect {
      [self.superview bringSubviewToFront:self];
    
      // Other code...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      相关资源
      最近更新 更多