iOS 屏幕旋转的设置方法

发布时间 2023-04-21 21:42:02作者: 滴水微澜
VC上屏幕旋转的方式有2种
1.因重力导致的屏幕旋转
条件:shouldAutorotate返回true,设备开启了屏幕旋转开关。
设备发生重力旋转。

2.单页面强制旋转
条件:无。
设置设备旋转方向。
NSNumber *orientationTarget = [NSNumber numberWithInteger:isLaunchScreen ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
其中重力旋转要满足的条件至少2个,AppDelegate代理设置和ViewController页面设置
而页面强制旋转只有满足AppDelegate代理设置就可以了。

旋转开关权限优先级
旋转开关权限优先级,决定了屏幕是否可以选择
a.AppDelegate代理 > App-General设备设置/info.plist 支持屏幕旋转, (App-General设备设置和info.plist是同步联动的,它们是APP启动前的预设,AppDelegate代理是App启动后的动态更改,可以覆盖前面启动前的设置)
b.ViewControll下的容器控制器也支持旋转,UINavigationController容器, UITabBarController容器设置的shouldAutorotate返回true
c.当前ViewController设置的shouldAutorotate返回true。

简单重力屏幕旋转设置
1.AppDelegate代理设置
// ,AppDelegate代理是App启动后的动态更改,可以覆盖启动前的设置,所以它的优先级最大
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if isLaunchScreen {
        return .landscapeRight
    } else {
        return .portrait
    }
}
2.ViewController页面设置
控制单个ViewController的旋转使用下面三个方法
// 1.是否支持屏幕旋转,只有返回true, 下面的两个方法才会执行
open var shouldAutorotate: Bool { get }

// 2.页面支持的屏幕方向
@available(iOS 6.0, *)
open var supportedInterfaceOrientations: UIInterfaceOrientationMask { get }

// 3.页面进入时的屏幕方向
@available(iOS 6.0, *)
open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { get }
复杂重力屏幕旋转设置
复杂重力屏幕旋转需要满足如下条件:
容器控制器的屏幕旋转范围要大于等于孩子控制器的屏幕旋转范围,否则配置传递中断,无法做重力旋转。
 
以普通的APP页面结构为例
通常的APP页面ViewController组织结构为:
UIApplication
window
它的根控制器rootViewController为UITabBarController

UITabBarController
下有多个子控制器,它们用UINavigationController包裹着
UINavigationController->UIViewController
UINavigationController->UIViewController
UINavigationController->UIViewController
UINavigationController->UIViewController
根控制器设置
UITabBarController容器控制器
verride var shouldAutorotate: Bool {
    return ((self.selectedViewController?.shouldAutorotate) != nil)
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return self.selectedViewController?.supportedInterfaceOrientations
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return self.selectedViewController?.preferredInterfaceOrientationForPresentation
}
UINavigationController容器控制器
override var shouldAutorotate: Bool {
    return self.topViewController?.shouldAutorotate

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return self.topViewController.supportedInterfaceOrientations
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return self.topViewController.preferredInterfaceOrientationForPresentation
}
单ViewController设置
// ViewController
override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscape
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeLeft
}  
屏幕旋转的常见使用场景
Modal出场方式因为没有了容器视图,可以剩去了中间2个容器的传递。

对于APP中主要页面竖屏,部分页面横屏的解决方法。
1.简单强制旋转
a.在AppDelegate和单ViewController设置好选择属性后,强制旋转
b.进入页面时强制横屏,退出时强制竖屏
 
2.复杂重力旋转
a.全局权限设置支持旋转的方向
b.自定义Tab控制器和Navigation控制器,处理子ViewController与容器ViewController的旋转配置传递
c.自定义ViewControl基类,添加基础旋转配置,
d.子类ViewControl中做自定义旋转配置。

旋转适配
ViewController的旋转适配
/* 
    This method is called when the view controller's view's size is changed by its parent (i.e. for the root view controller when its window rotates or is resized). 
    
    If you override this method, you should either call super to propagate the change to children or manually forward the change to children.
    */
@available(iOS 8.0, *)
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)

View的旋转适配
open func layoutSubviews() {
    if UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.landscapeLeft {
        旋转判断
    }
}
名称解释
1. UIDeviceOrientation 手机硬件的方向
2. UlInterfaceOrientation 手机页面的方向
3. UlInterfaceOrientationMask 手机页面支持的方向集合
4.UIDevice.current.orientation 获取手机硬件方向属性
 
 
参考文章:
https://www.jianshu.com/p/a354ca1890de
https://www.likecs.com/show-307967522.html