2020-04-23

/*

 获取顶层控制器的三种方案(兼容iOS 13)

 第1种和第2种方案有可能返回的是UITextEffectsWindow下的UIInputWindowController

 所以推荐使用第3种方案

 */

- (UIViewController *)getTopViewController1{

    __blockUIViewController*topVC =nil;

    if(@available(iOS13, *)) {

        NSSet *windowScenes = [UIApplication sharedApplication].connectedScenes;

        [windowScenesenumerateObjectsUsingBlock:^(UIWindowScene*windowScene,BOOL*_Nonnullstop) {

            if (windowScene.activationState == UISceneActivationStateForegroundActive)

            {

                topVC = windowScene.windows.lastObject.rootViewController;

                *stop =YES;

            }

        }];

    }else{

        topVC = [UIApplication sharedApplication].keyWindow.rootViewController;

    }

    while (topVC.presentedViewController) {

        topVC = topVC.presentedViewController;

    }

    returntopVC;

}

- (UIViewController *)getTopViewController2{

    UIViewController *appRootVC = [UIApplication sharedApplication].windows.lastObject.rootViewController;

    UIViewController*topVC = appRootVC;

    while (topVC.presentedViewController) {

        topVC = topVC.presentedViewController;

    }

    returntopVC;

}

- (UIViewController *)getTopViewController3{

    NSArray *windows = [UIApplication sharedApplication].windows;

    __blockUIViewController*topVC =nil;

    [windowsenumerateObjectsUsingBlock:^(UIWindow*window,NSUIntegeridx,BOOL*_Nonnullstop) {

        if([windowisKeyWindow]) {

            topVC = window.rootViewController;

            *stop =YES;

        }

    }];

    while (topVC.presentedViewController) {

        topVC = topVC.presentedViewController;

    }

    returntopVC;

}

你可能感兴趣的:(2020-04-23)