ios9 keyWindow为空 nil

问题:新window弹框消失后 keyWindow为null,造成面表现不正常。
解决方案:①重新获取keyWindow。

if(![[UIApplication sharedApplication] keyWindow]){
	UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
	[keyWindow makeKeyAndVisible];
}

或者写成共通方法

+ (UIWindow *)getKeyWindow{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0f){
        return [[[UIApplication sharedApplication] delegate] window];
    }else{
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            return [[[UIApplication sharedApplication] delegate] window];
        }
        return window;
    }
}

新window弹框部分代码

    UIViewController *tempRootVC = [[UIViewController alloc] init];
    if(@available(ios 13.0, *)){
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        tempRootVC = window.rootViewController;
    }else{
        UIWindow *tempWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        tempWindow.rootViewController = tempRootVC;
        [tempWindow makeKeyAndVisible];
    }
    [tempRootVC presentViewController:alert animated:YES completion:nil];

②临时window的弹框关闭时,设置keywindow

UIAlertAction *action = [UIAlertAction actionWithTitle:@"button1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [[[UIApplication sharedApplication] delegate].window makeKeyAndVisible];
            }];
            [alert addAction:action];

猜你喜欢

转载自blog.csdn.net/m0_46728513/article/details/105485026