美文网首页
iOS屏幕旋转完美解决方案

iOS屏幕旋转完美解决方案

作者: JD_W | 来源:发表于2017-10-08 15:58 被阅读0次

相信很多APP大多数页面都是竖屏,而只有部分页面会旋转,播放器是其代表之一。

但是工程的Landscape Left 和Landscape Right去掉,则部分页面也不支持旋转,Landscape Left 和Landscape Right开启又导致默认是的转屏,所以我们需要自己来实现逻辑控制界面是否可转并且还要默认是竖屏

废话也不多说,我来说说我的解决方案。

在设置的过程中首先提几点要求:

1、默认的所有页面是竖屏的

2、部分页面可以通过设置开关支持旋转。

解决思路:

1、首先我们先想到的是将plist的Landscape Left 和Landscape Right关掉,但是关掉了工程的配置,在单个页面开启旋转是行不通的。

2、那我们就不靠工程的配置,自己写逻辑来控制,在iOS6之后AppDelegate里面有个

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window

该方法在每次屏幕旋转的时候可以设置页面是否转屏。

新建个工具类AutoRotate.h具体代码逻辑如下


+ (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

if (window != nil) {

UIWindow *w = [UIApplication sharedApplication].delegate.window;

return [self orientationFromWindow:w];

}

return UIInterfaceOrientationMaskPortrait;

}

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

}

+ (BOOL)shouldAutorotate:(UIViewController *)showViewController {

//从方法列表中判断是否实现了shouldAutorotate方法(因为ViewController里面的实现不靠谱)

Class rotateClass = [showViewController class];

while (rotateClass != nil && rotateClass != [UIViewController class] && [rotateClass isSubclassOfClass:[UIViewController class]]) {

unsigned int methodCount;

Method *methods = class_copyMethodList(rotateClass, &methodCount);

for (unsigned int i = 0; i < methodCount; i++) {

SEL sel = method_getName(methods[i]);

if (sel == @selector(shouldAutorotate)) {//如果实现了方法,判断是否可转屏

return [showViewController shouldAutorotate];

}

}

rotateClass = [rotateClass superclass];

}

return NO;

}


如果你的APP是tabbar布局可以直接用,如果不是则需要更改rootViewController的转换


相关文章

  • iOS屏幕旋转完美解决方案

    相信很多APP大多数页面都是竖屏,而只有部分页面会旋转,播放器是其代表之一。 但是工程的Landscape Lef...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • iOS屏幕旋转解决方案

    1.导航控制器栈内部的VC方向是导航控制器来决定的。nav --- A --- B --- C,C的旋转方法是不起...

  • OC - 屏幕旋转(自动旋转、手动旋转、兼容iOS6之前系统)

    导读: 一、iOS6之前屏幕旋转知识了解 二、iOS6(包括iOS6)之后屏幕旋转知识了解 三、自动旋转具体操作 ...

  • 屏幕旋转和弹出框

    iOS中控制屏幕旋转相关方法 shouldAutorotate:是否支持屏幕旋转 alertView:clicke...

  • iOS屏幕旋转的解决方案

    这段时间在做跟视频有关的工作,视频嘛,肯定不是小窗口播放,在手机上播放视频肯定就是最大屏幕利用率啦,这就会用到全屏...

  • iOS Rotation

    iOS屏幕旋转学习笔记iOS开发中使用屏幕旋转功能的相关方法 1、基本知识点解读 了解屏幕旋转首先需要区分两种 o...

  • iOS传感器:实现一个随屏幕旋转的图片

    iOS传感器:实现一个随屏幕旋转的图片 iOS传感器:实现一个随屏幕旋转的图片

  • iOS 屏幕旋转处理

    iOS 屏幕旋转处理 在 iOS 设备上, 屏幕旋转是个挺坑比的问题. 苹果希望 app 对整个 app 做统一的...

  • iOS屏幕旋转

    方式一 假旋转 修改view的transform,通过旋转来实现横屏 工程配置 重写shouldAutorotat...

网友评论

      本文标题:iOS屏幕旋转完美解决方案

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