iOS7 ViewControllers 将 subView 添加到 topViewController 后不会出现

Posted

技术标签:

【中文标题】iOS7 ViewControllers 将 subView 添加到 topViewController 后不会出现【英文标题】:iOS7 ViewControllers won't appear after adding subView to topViewController 【发布时间】:2013-10-08 07:44:54 【问题描述】:

我更改了应用程序在进入后台时的行为,因此它会拍摄快照,对其应用模糊效果,然后将 UIImageView 推送到 rootViewController 的子视图中。 它在设备上完美运行(不是在模拟器上......),但是当我回来时,我的导航控制器的按钮停止工作。如果我将应用程序置于后台,然后再次返回,我会看到我点击的控制器。

这是我的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    UINavigationController* topViewController = (UINavigationController*)window.rootViewController.topViewController;
    NSLog(@"%@",topViewController.class);
    UIImage* snapshot = [[topViewController topViewController].view takeSnapshot];
    overlay = [[UIImageView alloc] initWithImage:[snapshot applyDarkEffect]];
    [[topViewController topViewController].view addSubView:overlay];

applyDarkEffect 是来自苹果示例应用程序的类别,而 takeSnapshot 是我在堆栈溢出时找到的类别。

当我的应用恢复活力时的代码:

- (void)applicationWillEnterForeground:(UIApplication *)application

    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    [overlay removeFromSuperview];

叠加层是我的应用委托的属性;

takeSnapshot 属于 UIView 的类别:

- (UIImage *)takeSnapshot 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);

    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];

    // old style [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
 

另外,我的 rootViewController 是 UINavigationController

【问题讨论】:

【参考方案1】:

我找到了问题!

“新”样式捕获导致问题:[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];

我不得不使用“旧”样式:[self.layer renderInContext:UIGraphicsGetCurrentContext()]

【讨论】:

以上是关于iOS7 ViewControllers 将 subView 添加到 topViewController 后不会出现的主要内容,如果未能解决你的问题,请参考以下文章

Xcode:如何将对象从一个 viewControllers 视图移动到另一个 viewControllers 视图?

将相同的 UITableView 添加到不同的 ViewControllers

如何从 navigationController(viewControllers/stack) 弹回指定 viewController?

在 ViewControllers 之间共享信息 [重复]

设置 NavigationController.Viewcontrollers 崩溃(使用自定义转换)

Typhoon:Assembly 和 Storyboard 创建的 ViewControllers