iOS模拟器的“颜色离屏渲染”功能有问题吗?

4
我想知道我的应用程序中哪个视图是离屏渲染的。因此,我使用iOS模拟器的“颜色离屏渲染”功能,可以将那些离屏渲染的视图标记成黄色。但是,在应用程序启动后,整个屏幕都被涂成了黄色,我不敢相信这个结果。
然后,我尝试了我的测试代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];

    self.window.rootViewController = [[UITabBarController alloc] init];
    //    self.window.rootViewController = [[UINavigationController alloc] init];
    //    self.window.rootViewController = [[UIViewController alloc] init];

    [self.window makeKeyWindow];
}

正如您在上面看到的,我只是通过原始控制器三次不同地设置了窗口的rootViewController: 'UITabBarController'、'UINavigationController'和'UIViewController'。

你猜怎么着?

只有'UIViewController'没有整个屏幕着色!!!

enter image description here

那么,有人知道为什么原始的rootViewController和UINavigationController会出现整个屏幕的离屏渲染吗?

1个回答

3
这是因为UITabBarUINavigationBar的默认translucent值为YES。您应该查看苹果文档以获取有关UINavigationBar.translucentUITabBar.translucent的更多信息。通过创建一个带有红色背景根视图控制器的UINavigationController的小演示,我们可以比较当translucentYESNO时的差异。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  self.window.backgroundColor = [UIColor whiteColor];

  UIViewController* viewController = [[UIViewController alloc] init];
  viewController.view.backgroundColor = [UIColor redColor];
  UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
//  nav.navigationBar.translucent = NO;
  self.window.rootViewController = nav;

  [self.window makeKeyWindow];
  return YES;
}

默认情况下,透明度为YES。因此您可以看到UINavigationBar的背景有一点红色。

enter image description here

离屏渲染颜色

enter image description here

但是当我们将translucent设置为NO时,导航栏的背景不再是红色。

enter image description here

离屏渲染颜色

enter image description here


我们这里使用了透明度,所以屏幕会被着色。你可以尝试使用UITabBartranslucent实现类似的效果。
为了避免UINavigationControllerUITabBarController的离屏渲染,应将此属性设置为NO

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接