美文网首页
关于IOS屏幕旋转的问题

关于IOS屏幕旋转的问题

作者: 代码DJ | 来源:发表于2018-03-19 15:41 被阅读0次

IOS 开发中,屏幕旋转是比较蛋疼的。网络上有很多的关于屏幕旋转的文章,都比较碎片,而且有些做法还很暴力,并且在应用提交审核的过程中有些实现代码是不被审核通过的。

这里介绍一下我在接受公司外包出去的项目,拿回来重构的过程中,遇到的屏幕旋转的问题。由于我是Android开发,公司有需求,我就转过来做点IOS的开发。新手,所以这样的问题应该IOS多年开发经验的开发人员可以绕道了。

解决IOS屏幕旋转比较优雅的方式,可以看一下下面的一个操作流程图:

代码执行流程

这里简单的描述了一下屏幕旋转的代码执行流程:
首先,当应用启动之后,会执行一个回调:


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

这个方法用于控制应用启动后的初始化屏幕方向,也包括控制整个应用软键盘弹出的方向。如果这个回调函数中,返回了


UIInterfaceOrientationMaskPortrait //应用屏幕竖向显示
UIInterfaceOrientationMaskLandscapeRight //屏幕横向显示

那么正常的情况下,我们可以这么做,在上述的AppDelegate的回调函数supportedInterfaceOrientationsForWindow中写入如下的代码,那么就能让单个页面来决定自己的屏幕方向了。


- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  return self.navigation.topViewController.preferredInterfaceOrientationForPresentation;
}

然后在所有的子ViewController中实现如下三个方法,这样,就可以轻松自如的切换每个单个界面控制器的屏幕方向了。


- (BOOL)shouldAutorotate{
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  return UIInterfaceOrientationLandscapeRight;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  return UIInterfaceOrientationMaskLandscapeRight;
}

相关文章

网友评论

      本文标题:关于IOS屏幕旋转的问题

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