#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;
}
网友评论