[英]IOS/Autolayout: Change to Presenting View After Presented Controller Dismissed
[英]Presented view controller gets dismissed if i move my application to background and then come to foreground in iOS
当用户将应用程序移动到后台applicationDidEnterBackground时,我需要用黑色布局覆盖屏幕,以维护屏幕上某些敏感数据的隐私。 因此,为此我使用 AppDelegate 函数在背景中呈现黑色,然后通过在前景applicationDidEnterBackground 中关闭它来删除它。 代码是:
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIViewController *blankViewController = [UIViewController new];
blankViewController.view.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:blankViewController animated:NO completion:NULL];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self.window.rootViewController dismissViewControllerAnimated:false completion:nil];
}
@end
现在在我的应用程序中一切正常,但在一个屏幕中,我通过使用以下按钮单击一个ViewContollerB [self presentViewController:webview animated:YES completion:nil];
当移动到背景时,应用程序通常会被黑色覆盖,但是当我在此之后将应用程序带到前台时,所呈现的ViewContollerB也会被驳回。 如何防止我呈现的 ViewController 一旦来自后台就被解雇?
您正在尝试执行以下操作:rootviewcontroller A 呈现一个空白的 viewcontroller B。您的应用程序进入后台,然后您从那里呈现 viewcontroller C。 现在 B 关闭,因为 A 同时呈现 B 和 C,这是不允许的。
您将需要检查您的 rootviewcontroller 是否正在呈现任何视图控制器,以及这些是否以递归方式呈现任何其他视图控制器。 您可以通过使用视图控制器上的属性presentedViewController
来检查这些。
就个人而言,我会(在所有 viewcontroller 继承的基础 viewcontroller 类中)保留一个变量来检查这个变量是否可见(通过跟踪viewDidAppear
和viewDidDisappear
)。 如果这是可见的,则在顶部添加一个空白视图。
用 Swift 回答,因为如果没有编辑器,我无法信任 Objective-C:
class BaseViewController: UIViewController {
var appeared = false
func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated: animated)
appeared = true
}
func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated: animated)
appeared = false
}
}
然后你需要从这个视图AppDelegate
中捕获的AppDelegate
触发一个通知,然后在顶部显示一个空白视图。
创建一个名为OverLayViewController
的新“UIViewController”并将其加载到applicationDidEnterBackground
方法中
- (void)applicationDidEnterBackground:(UIApplication *)application {
OverLayViewController *blankViewController = [OverLayViewController new];
blankViewController.view.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
UIViewController *rvc = self.window.rootViewController;
UIViewController *pvc = rvc.presentedViewController; // you may need to loop through presentedViewControllers if you have more than one
if(pvc != nil) {
[pvc presentViewController: blankViewController animated: NO completion:nil];
}
else{
[self.window.rootViewController presentViewController: blankViewController animated: NO completion:nil];
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
UIViewController *test = [self topViewController];
if ([test isKindOfClass:[OverLayViewController class]]) {
[test dismissViewControllerAnimated:false completion:nil];
}
}
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
-(UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
在这种情况下, dismissViewContoller
代码不适用于您的dismissViewContoller
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.