仅当iOS7上的hidesBottomBarWhenPushed设置为YES时,UITabBarController子视图才会消失 [英] UITabBarController subviews disappear when hidesBottomBarWhenPushed is set to YES only on iOS7

查看:123
本文介绍了仅当iOS7上的hidesBottomBarWhenPushed设置为YES时,UITabBarController子视图才会消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a subclass of UITabBarController that adds a custom UIView on top of tabs to replace the default badging on a UITabBarItem. I add these custom badgeviews on viewDidLoad of my custom UITabBarController.

[self.view addSubview:_tabBarItem1BadgeView];

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

The drawRect on my custom tabBarBadgeView looks like this:

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

效果很好。我可以看到徽章查看我添加它的确切位置。如果我切换选项卡,没有任何影响。

Works great. I can see a badgeView exactly where I add it.If i switch around tabs, nothing is affected.

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

All of the tabControllers's view controllers are UINavigationControllers. I have one use case where one tab's UINavigationController's top most UIViewController should not show the tabBar, so I naturally set

controller.hidesBottomBarWhenPushed = YES

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

before pushing on the navigationController stack. This successfully suppresses the tabBar, but my custom UIViews continue to exist. To fix this, I made my custom UITabBarController a UINavigationControllerDelegate. This allows me to manually hide and show those custom UIViews when the navigation pushes and pops.

我这样做是使用:

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

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

效果很好....仅限iOS6。

Works great....only in iOS6.

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

On iOS7, the custom UITabBarController will not show the custom UIViews after popping back from a UIViewController that has hidesBottomBarWhenPushed set to YES. If hidesBottomBarWhenPushed is set to NO, the UIViews continue to appear.

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

In fact, I removed the UINavigationControllerDelegate entirely, and the odd thing happening is that when I drill down the stack on UINavigationController (with hidesBottomBarWhenPushed=YES), the tabBar is hidden, but the custom UIViews I added remain (something I expected). But when I pop back from that (and this is the oddest part), I get back to the tab's top level controller (which should show the tabBar), and the tabBar is visible(expected), but the custom UIViews disappear. Click back down, they appear, press back, they disappear.

此行为仅发生在iOS7上。在显示带有hidesBottomBarWhenPushed = YES的UIViewController,然后返回到带有hidesBottomBarWhenPushed = NO的UIViewController后,UITabBar会发生什么变化?

And this behavior only happens on iOS7. Is there something that happens to the UITabBar differently after showing a UIViewController with hidesBottomBarWhenPushed = YES and then going back to a UIViewController with hidesBottomBarWhenPushed = NO?

推荐答案

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

Your custom badge view is getting hidden behind UITabBarController's own views whenever UITabBarController re-adds its own views to its hierarchy.

要始终将自定义徽章视图保持在最顶层,您可以在自定义徽章视图上覆盖 drawRect 并调用 [self.superview bringSubviewToFront:self];

To keep your custom badge view always on top, you can override drawRect on your custom badge view and call [self.superview bringSubviewToFront:self];

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

  // Other code...
}

这篇关于仅当iOS7上的hidesBottomBarWhenPushed设置为YES时,UITabBarController子视图才会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆