iOS屏幕旋转那些事

作者: AliThink | 来源:发表于2016-08-02 11:35 被阅读634次

本文屏幕旋转方案仅限于兼容iOS8+

1. 从APP层次谈起

APP常见的布局层次如下图所示:

app layer.png
当工程目标开启了多个屏幕方向之后,具体类中有关屏幕旋转的配置其实只与当前屏幕展示模块的最外层VC容器有关。
但由于往往存在容器中某些VC针对屏幕旋转的个性化配置,所以需要进行从里层VC到外层容器的旋转状态传递。例如:
  • 里层UIViewController的配置
override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Portrait
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return .Portrait
}
  • 自定义UINavigationController的配置
override public func shouldAutorotate() -> Bool {
    return self.viewControllers.last?.shouldAutorotate() ?? false
}

override public func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return self.viewControllers.last?.supportedInterfaceOrientations() ?? .Portrait
}

override public func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return self.viewControllers.last?.preferredInterfaceOrientationForPresentation() ?? .Portrait
}
  • 自定义UITabBarController的配置
override func shouldAutorotate() -> Bool {
    return self.selectedViewController?.shouldAutorotate() ?? false
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return self.selectedViewController?.supportedInterfaceOrientations() ?? .Portrait
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return self.selectedViewController?.preferredInterfaceOrientationForPresentation() ?? .Portrait
}

2. AppDelegate配置

按照上面的方法配置好后,就可以自由的控制转屏了,但在某些情况下会存在问题,举个例子:假如A页面屏幕锁定为竖屏,点击A页面的一个按钮跳转到了B页面(方式存在push跟present两种),B页面是可以进行横竖屏旋转的,当B页面旋转至横屏,这时候点击返回,会发现A页面也变成横屏展示了,而且无法通过屏幕旋转恢复到竖屏展示。这时候下面这个代理方法就排上用场了:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {    
    // 限定所有present的模态视图只能竖屏
    if self.window?.rootViewController?.presentedViewController != nil {
        return .Portrait
    }
    
    // 限定容器中当前展示的为RotateDetailViewController的实例时可以旋屏,其余页面只能竖屏
    let baseTabBarController = self.window?.rootViewController as? BaseTabBarController
    if ((baseTabBarController?.selectedViewController as? BaseNavigationController)?.topViewController is RotateDetailViewController {
        return .AllButUpsideDown
    } else {
        return .Portrait
    }
}

3. 强制旋屏

前提条件:该VC允许旋转

  • 强制竖屏
func forcePortrait(){
    let width = UIScreen.mainScreen().bounds.size.width
    let height = UIScreen.mainScreen().bounds.size.height
    
    let isLandscape = width > height
    if isLandscape {
        let device = UIDevice.currentDevice()
        let number = NSNumber(integer: UIInterfaceOrientation.Portrait.rawValue)
        device.setValue(number, forKey: "orientation")
    }
}
  • 强制横屏
func forceLandScape(){
    let width = UIScreen.mainScreen().bounds.size.width
    let height = UIScreen.mainScreen().bounds.size.height
    
    let isLandscape = width < height
    if isLandscape {
        let device = UIDevice.currentDevice()
        let number = NSNumber(integer: UIInterfaceOrientation.LandscapeRight.rawValue)
        device.setValue(number, forKey: "orientation")
    }
}

总结

以上方案基本可以搞定大部分的屏幕旋转场景了。如果APP的产品需求中主体为固定方向,只要求对弹出(present)模态视图进行旋转的话,可以参考下面这篇文章:
iOS Orientations: Landscape orientation for only one View Controller
这么做会更加方便一些,但问题是这种方案对于push模式的场景并不适用,需要结合以上方案综合解决。

相关文章

  • iOS屏幕旋转那些事

    本文屏幕旋转方案仅限于兼容iOS8+ 1. 从APP层次谈起 APP常见的布局层次如下图所示: 里层UIViewC...

  • IOS屏幕旋转的那些事

    一、页面随着屏幕旋转 1、先确保XCode中Deployment Info设置如下 2、其次在需要旋转的ViewC...

  • 屏幕旋转

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

  • 屏幕旋转的那些事

    屏幕旋转一般有三种方式: 第一种、系统自带的自动旋转,只需要在Xcode - General设置即可 第二种、 U...

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

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

  • 屏幕旋转和弹出框

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

  • iOS Rotation

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

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

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

  • iOS 开发_屏幕旋转的那些坑儿事

    【作者前言】:13年入圈,分享些本人工作中遇到的点点滴滴那些事儿,17年刚开始写博客,高手勿喷!以分享交流为主,欢...

  • iOS 屏幕旋转处理

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

网友评论

    本文标题:iOS屏幕旋转那些事

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