(资料图)

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返回truec.当前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组织结构为:UIApplicationwindow它的根控制器rootViewController为UITabBarControllerUITabBarController下有多个子控制器,它们用UINavigationController包裹着
UINavigationController->UIViewControllerUINavigationController->UIViewControllerUINavigationController->UIViewControllerUINavigationController->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?.shouldAutorotateoverride var supportedInterfaceOrientations: UIInterfaceOrientationMask {    return self.topViewController.supportedInterfaceOrientations}override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {    return self.topViewController.preferredInterfaceOrientationForPresentation}
单ViewController设置
// ViewControlleroverride 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/a354ca1890dehttps://www.likecs.com/show-307967522.html

推荐内容