+ (UIInterfaceOrientationMask)orientationFromWindow:(UIWindow *)window {
UIViewController *rootViewController = window.rootViewController;
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabar = (UITabBarController *)rootViewController;
//获取当前选择的navigationController
UINavigationController *nav = tabar.selectedViewController;
//获取当前显示的viewController
UIViewController *topViewController = nav.topViewController;
//获取当前界面的presentedViewController
UIViewController *presentedViewController = topViewController.presentedViewController;
//正在显示的viewController
UIViewController *showViewController = topViewController;
//如果有presentedViewController,则用
if (presentedViewController != nil) {
//TODO 这个判断有待测试 下面的情况用的不多
if ([presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)presentedViewController;
UIViewController *topVC = nav.topViewController;
showViewController = topVC;
} else {
showViewController = presentedViewController;
}
}
//默认不转屏
UIInterfaceOrientationMask orientation = UIInterfaceOrientationMaskPortrait;
BOOL shouldAutorotate = [self shouldAutorotate:showViewController];
//如果可转屏,获取转屏方向
if (shouldAutorotate) {
orientation = [showViewController supportedInterfaceOrientations];
} else {
UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
switch (statusBarOrientation) {
case UIInterfaceOrientationLandscapeLeft:
orientation = UIInterfaceOrientationMaskLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeRight:
orientation = UIInterfaceOrientationMaskLandscapeRight;
break;
default:
orientation = UIInterfaceOrientationMaskPortrait;
break;
}
}
return orientation;
}
return UIInterfaceOrientationMaskPortrait;
}