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

时间:2013-10-08 07:44:55

标签: ios viewcontroller navigationcontroller

当我的应用程序转到后台时,我已经改变了它的行为,因此需要快照,对其应用模糊效果,然后将UIImageView推送到rootViewController的子视图中。 它在设备上完美运行(不在模拟器上......),但是当我回来时,我的navigationController的按钮停止工作。如果我把应用程序放到后台,然后又回来,我看到我点击的控制器。

这是我的代码:

- (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是apple的示例应用程序中的类别,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 个答案:

答案 0 :(得分:0)

我发现了问题!

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

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