2020-04-23

作者: coderxiaoqin | 来源:发表于2020-04-23 20:44 被阅读0次

    /*

     获取顶层控制器的三种方案(兼容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

        本文链接:https://www.haomeiwen.com/subject/ihezihtx.html