iOSSceneDelegate

iOSSceneDelegate

920 发布: 2021/7/22 09:57 本文总阅读量

iOS 13 新特性之 UIWindowScene

-(UIWindow *)getRootWindow {
    if (@available(iOS 13.0,*)) {
        /*
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow *window in [windows reverseObjectEnumerator]) {
            if ([window isKindOfClass:[UIWindow class]] &&
                window.windowLevel == UIWindowLevelNormal &&
                CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds) &&
                window.hidden == NO){
                return window;
            }
        }
        */
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
            if (windowScene.activationState == UISceneActivationStateForegroundActive) {
                NSLog(@"get==:%@",windowScene.windows);
                for (UIView *view in windowScene.windows) {
                    if ([view isKindOfClass:[UIWindow class]]) {
                        UIWindow *window = (UIWindow *)view;
                        return window;
                    }
                }
            }
        }
        return [UIApplication sharedApplication].delegate.window;
    }else {
        return [UIApplication sharedApplication].delegate.window;
    }
}

拿到window就可以按照之前的方法进行重置跟控制器了:

 UIWindow *window = [self getRootWindow];
 window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];