ios 旋转屏幕试图切换_iOS屏幕旋转完美解决方案

本文提供了一个解决iOS应用中屏幕旋转时试图切换的问题的方案。通过获取当前显示的UIViewController并检查其旋转支持,实现了在不同界面间的智能旋转。在有presentedViewController时,也能正确处理旋转方向。
摘要由CSDN通过智能技术生成

+ (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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值