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
 

iOS 屏幕旋转的设置方法的更多相关文章

  1. iOS屏幕旋转 浅析

    一.两种orientation 了解屏幕旋转首先需要区分两种orientation 1.device orientation 设备的物理方向,由类型UIDeviceOrientation表示,当前设备 ...

  2. 【转】IOS屏幕旋转与View的transform属性之间的关系,比较底层

    iTouch,iPhone,iPad设置都是支持旋转的,如果我们的程序能够根据不同的方向做出不同的布局,体验会更好. 如何设置程序支持旋转呢,通常我们会在程序的info.plist中进行设置Suppo ...

  3. IOS屏幕旋转思路和实践

    这段时间同事在做一个直播项目,项目有个需求:一个界面需要手动设置屏幕的方向,设置好之后方向不能变化.完成这个需求花了特别大的精力,归因是网上关于屏幕旋转的知识比较凌乱,解决问题花费不少时间,最后决定把 ...

  4. ios 屏幕旋转的问题

    在ios6之前我们旋转屏幕只需要实现shouldAutorotateToInterfaceOrientation就行了 - (BOOL)shouldAutorotateToInterfaceOrien ...

  5. iOS 屏幕旋转 nav+tabbar+present(网页) 2016

    如题,最近一个app架构为 nav + tabbar ,需求是 在点击tabbar中的一个菜单项时,弹出网页,该网页需要横屏显示,其他页面不变  都保持竖屏. XCode Version 7.2.1 ...

  6. ios 屏幕方向的设置

    ref: http://www.cnblogs.com/niit-soft-518/p/5611298.html 实际的项目需求.root是TabBarController,里面有4个navigati ...

  7. iOS屏幕旋转

    三种方法 需求:全局主要是竖屏 个别界面需要横屏

  8. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  9. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  10. 【转】IOS设备旋转的内部处理流程以及一些【优化建议】

    加速计是整个IOS屏幕旋转的基础,依赖加速计,设备才可以判断出当前的设备方向,IOS系统共定义了以下七种设备方向: typedef NS_ENUM(NSInteger, UIDeviceOrienta ...

随机推荐

  1. Excel的列数如何用数字表示?

      本文介绍在Excel表格文件中,用数字而非字母来表示列号的方法.   在日常生活.工作中,我们不免经常使用各种.各类Excel表格文件:而在Excel表格文件中,微软Office是默认用数字表示行 ...

  2. redis管道技术pipeline二——api

    package spring.redis; import org.springframework.beans.factory.InitializingBean; import org.springfr ...

  3. 上海丨阿里云 Serverless 技术实战营邀你来玩!

    活动简介 本次沙龙深度探讨 "Serverless 在中国企业的落地和开发者实操" 主题,我们特别邀请了来自阿里云 一线技术专家,分享当前 Serverless 趋势和落地实践过程 ...

  4. 三、docker容器的常用命令

    系列导航 一.docker入门(概念) 二.docker的安装和镜像管理 三.docker容器的常用命令 四.容器的网络访问 五.容器端口转发 六.docker数据卷 七.手动制作docker镜像 八 ...

  5. 理解 Kubernetes volume 和 共享存储

    1. Kubernetes volume 文章 介绍了 Docker volume.与 docker volume 类似的,在 kubernetes 中存在 Pod 级别的 volume,Pod 的 ...

  6. Ubuntu安装jdk的步骤

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  7. AMBA总线介绍-02

    AMBA总线介绍 1 HSIZE AHB总线的地址位宽和数据位宽一般都是32bit,一个字节8bit,一个字节占用一个地址空间,但当一个32bit的数据写入一个存储器中或者从一个存储器中读取,32bi ...

  8. Go-数组-切片

  9. [转帖]Kafka Dashboard

    https://grafana.com/grafana/dashboards/18276-kafka-dashboard/ Kafka resource usage and consumer lag ...

  10. [转帖]从理论到实践,异步I/O模式下NVMe SSD高性能之道

    在早期NVMe的讨论话题中,常常将之AHCI协议进行对比,在支持的最大队列深度.并发进程数以及消耗时钟周期数等方面,NVMe吊打了AHCI.最直观也最权威的就是下面这张对比图片. NVMe与AHCI协 ...