美文网首页iOS 学习
iOS 获取window根视图(rootViewControll

iOS 获取window根视图(rootViewControll

作者: 零粹 | 来源:发表于2019-01-17 14:55 被阅读0次
    #pragma mark - 获取window根视图
    - (UIViewController *)rootViewController{
        if (!_rootViewController) {
            _rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        }
        return _rootViewController;
    }
    
    #pragma mark - 获取window顶层的UIViewController
    - (UIViewController *)topViewController {
        
        _topViewController = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
        while (_topViewController.presentedViewController) {
            _topViewController = [self _topViewController:_topViewController.presentedViewController];
        }
        
        return _topViewController;
    }
    
    - (UIViewController *)_topViewController:(UIViewController *)vc {
        if ([vc isKindOfClass:[UINavigationController class]]) {
            return [self _topViewController:[(UINavigationController *)vc topViewController]];
        } else if ([vc isKindOfClass:[UITabBarController class]]) {
            return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
        } else {
            return vc;
        }
        return nil;
    }
    
    

    相关文章

      网友评论

        本文标题:iOS 获取window根视图(rootViewControll

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