转自 http://blog.csdn.net/zzfsuiye/article/details/8251060

概述:

在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewController支持旋转,只需要重写shouldAutorotateToInterfaceOrientation方法。

但是iOS 6里屏幕旋转改变了很多,之前的 shouldAutorotateToInterfaceOrientation 被列为 DEPRECATED 方法,查看UIViewController.h文件也可以看到:

[cpp] view plaincopy
  1. // Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
  2. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);

程序将使用如下2个方法来代替:

[cpp] view plaincopy
  1. - (BOOL)shouldAutorotate;
  2. - (NSUInteger)supportedInterfaceOrientations;

除了重写这个2个方法,IOS6里面要旋转还有一些需要注意的地方,下面会细述。另外还有一个硬性条件,需要在Info.plist文件里面添加程序支持的所有方向,可以通过以下2种方式添加

1.

2.

另外要兼容IOS6之前的系统,要保留原来的 shouldAutorotateToInterfaceOrientation 方法,还有那些 willRotateToInterfaceOrientation 等方法。

IOS6自动旋转设置:

[cpp] view plaincopy
  1. UIViewController *viewCtrl = [[UIViewController alloc] init];
  2. UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
  3. if ([window respondsToSelector:@selector(setRootViewController:)]) {
  4. self.window.rootViewController = navCtrl;
  5. } else {
  6. [self.window addSubview:navCtrl.view];
  7. }

如果需要设置viewCtrl的旋转,那么不能在UIViewController里面重写shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面设置,又因为UINavigationController是系统控件,所以这里需要新建一个UINavigationController的子navigationController的子类,然后在里面实现shouldAutorotate和supportedInterfaceOrientations方法,比如:

[cpp] view plaincopy
  1. -(NSUInteger)supportedInterfaceOrientations{
  2. return UIInterfaceOrientationMaskAllButUpsideDown;
  3. }
  4. - (BOOL)shouldAutorotate{
  5. return YES;
  6. }
eg1:如果上面的例子是self.window.rootViewController = viewCtrl,而不是navCtrl,那么上面的那2个控制旋转的方法就应该写在UIViewController里面!

eg2:如果viewCtrl又pushViewController到viewCtrl2,需要设置viewCtrl2的旋转,怎么办呢? 还是在navCtrl里面控制,因为viewCtrl和viewCtrl2的rootViewController都是navCtrl,一般的写法都是
[cpp] view plaincopy
  1. UIViewController *viewCtrl2 = [[UIVewController alloc] init];
  2. [self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];
所以要控制一个UINavigationController push到的所有viewController的旋转,那么就得在navCtrl里面区分是哪个viewController,以便对他们一一控制!同样如果rootViewController是UITabbarController,那么需要在子类化的UITabbarController里面重写那2个方法,然后分别控制!
但是有时候我初始化UINavigationController的时候,并不知道所有我所有需要push到的viewController,那么这里有一个通用的方法,就是让viewController自己来控制自己,首先在navCtrl里面的实现方法改为以下方式:
[cpp] view plaincopy
  1. - (BOOL)shouldAutorotate
  2. {
  3. return self.topViewController.shouldAutorotate;
  4. }
  5. - (NSUInteger)supportedInterfaceOrientations
  6. {
  7. return self.topViewController.supportedInterfaceOrientations;
  8. }

全部调用self.topViewController,就是返回当前呈现出来的viewController里面的设置,然后在viewCtrl、viewCtrl2等等这些viewController里面重写shouldAutorotate和supportedInterfaceOrientations,以方便设置每个viewController的旋转


eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那么viewCtrl3的旋转设置就不在navCtrl里面了!如果presentModalViewController的viewController是navController、tabbarController包装过的viewCtrl3,那么就应在新包装的navController、tabbarController里面设置,如果是直接presentModalViewController到viewCtrl3,那么就在viewCtrl3里面设置

IOS5、IOS4自动旋转设置

这个简单很多,没有上面的硬性条件,只需要在需要旋转的viewController里面重写 shouldAutorotateToInterfaceOrientation 方法就行

手动旋转

手动旋转也有2种方式,一种是直接设置 UIDevice 的 orientation,但是这种方式不推荐,上传appStore有被拒的风险:

[cpp] view plaincopy
  1. if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
  2. [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
  3. }

第二种是假旋转,并没有改变 UIDevice 的 orientation,而是改变某个view的 transform,利用 CGAffineTransformMakeRotation 来达到目的,比如:

[cpp] view plaincopy
  1. self.view.transform = CGAffineTransformMakeRotation(M_PI/2)

下面讲解采用第二种方式的各版本手动旋转:

思想是首先设置 statusBarOrientation,然后再改变某个view的方向跟 statusBarOrientation 一致!

IOS6手动旋转:

1. 那既然是旋转,最少也得有2个方向,那么还是少不了上面说的那个硬性条件,先在plist里面设置好所有可能需要旋转的方向。既然是手动旋转,那么就要关闭自动旋转:

[cpp] view plaincopy
  1. - (BOOL)shouldAutorotate{
  2. return NO;
  3. }

2.手动触发某个按钮,调用方法,这个方法的实现如下:

[cpp] view plaincopy
  1. [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
  2. self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
  3. self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);

注意:
1. 只需要改变self.view.transform,那么self.view的所有subview都会跟着自动变;其次因为方向变了,所以self.view的大小需要重新设置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的话,下面设置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情况下才管用!

IOS5、IOS4手动旋转:

1.在需要手动旋转的viewController里的 shouldAutorotateToInterfaceOrientation 方法设置 interfaceOrientation == [UIApplicationsharedApplication].statusBarOrientation

[cpp] view plaincopy
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
  2. return (interfaceOrientation == [UIApplication sharedApplication].statusBarOrientation);
  3. }

2.手动触发某个按钮,调用方法,这个方法的实现如下:

[cpp] view plaincopy
  1. [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
  2. self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
  3. self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);

注意:只需要改变self.view.transform,那么self.view的所有subview都会跟着自动变;其次因为方向变了,所以self.view的大小需要重新设置,不要使用self.view.frame,而是用bounds。

经验分享:

1.IOS6里面,如果一个项目里面需要各种旋转支持,有自动,有手动,那么我们可以新建2个navController或者tabbarController的子类,一个是不旋转,一个旋转,那么所有需要旋转的UINavigationController都可以用这个子类来代替!包括我们可以定制短信呀、邮件呀的旋转!
2.supportedInterfaceOrientations 方法一般是写UIInterfaceOrientationMask方向,但是如果程序要兼容4.3以下的SDK(4.3以下的SDK必须是4.5以下的Xcode,不支持IOS6),那么在用4.5以下的Xcode编译的时候通不过!所以可以用statusBarOrientation代替或者直接写死数字!

[cpp] view plaincopy
  1. -(NSUInteger)supportedInterfaceOrientations{
  2. return [UIApplication sharedApplication].statusBarOrientation;
  3. }

3.一般都不建议在程序里面直接调用 UIDeviceOrientation 的方向,而是用 UIInterfaceOrientation,他们之间是不同的!

[cpp] view plaincopy
  1. UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
  2. UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft

看到吗,左右是反的!Xcode图形化设置方向也是以 UIInterfaceOrientation 为准,就是home按键所在的方向

参考:
http://blog.csdn.net/totogogo/article/details/8002173
http://stackoverflow.com/questions/13200220/how-to-change-keyboard-orientation-in-ios6
http://blog.csdn.net/yiyaaixuexi/article/details/8035014

IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)相关推荐

  1. 【学会轮播图这一篇文章就足够啦】JS 网页轮播图详解 自动播放+手动播放

    轮播图已经成了页面开发中不可缺少的一部分,日常生活中随处都能见到轮播图的身影,例如平常我们购物的淘宝,京东等等,都靠着轮播图在一片 有限的区域内展现出更多的商品.这也是前端程序员最早接触到的练手小项目 ...

  2. UIDeviceOrientation 和 UIInterfaceOrientation 设备旋转的用法 (实例)  和 IOS6屏幕旋转详解

    1. UIDeviceOrientation 和 UIInterfaceOrientation 设备旋转的用法 (实例) 博客分类: IOS / Objective-C UIDeviceOrienta ...

  3. 放大 旋转 css3,CSS3详解:transform 的旋转、旋转放大、放大、移动

    CSS3 transform是什么? transform的含义是:改变,使-变形:转换 CSS3 transform都有哪些常用属性? transform的属性包括:rotate() / skew() ...

  4. AVL树平衡旋转详解

    AVL树平衡旋转详解 概述 AVL树又叫做平衡二叉树.前言部分我也有说到,AVL树的前提是二叉排序树(BST或叫做二叉查找树).由于在生成BST树的过程中可能会出现线型树结构,比如插入的顺序是:1, ...

  5. CORDIC算法详解(一)- CORDIC 算法之圆周系统之旋转模式( Rotation Mode )

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/Pieces_thinking/arti ...

  6. Unity史上最全旋转详解(Rotate,rotation,localEulerAngles,localRotation,万向节锁)

    Unity史上最全旋转详解 前言 旋转的方法Rotate以及五种重载参数的超级详细理解 Rotate(float xAngle, float yAngle, float zAngle); Unity绕 ...

  7. android屏幕适配详解

    android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 一.关于布局适配建议 1.不要 ...

  8. 一文详解自动驾驶的动态驾驶任务(DDT) | 自动驾驶系列

    文章版权所有,未经授权请勿转载或使用 本系列上篇文章<一文详解自动驾驶的运行设计域(ODD)>解读了什么是自动驾驶ODD,本篇文章依据SAE J3016详细解读自动驾驶DDT.DDT fa ...

  9. 一文详解自动驾驶的运行设计域(ODD)| 自动驾驶系列 1

    一文详解自动驾驶的运行设计域(ODD)| \n 自动驾驶系列 2021年4月30日,SAE发布了第四版J3016<驾驶自动化分级>,这是即2014年1月16日.2016年9月30日.201 ...

最新文章

  1. 30款最好的 Bootstrap 3.0 免费主题和模板
  2. Flexible 弹性盒子模型之flex
  3. Python递归文件夹遍历所有文件夹及文件
  4. idea 导入到码云
  5. 自学python后自己接单-自学python后,可以自己独立做什么事情来挣钱吗?
  6. 使用javadoc命令制作帮助文档(API)
  7. Object-C——三大特性之多态
  8. 亚信科技外包_外包到亚信---转正疑问 - 菜鸟@大虾的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  9. 算法笔记(个人用)(不定期更新)
  10. c语言tc2.0编译器下载,c语言tc2.0下载
  11. 防火墙之ASDM配置虚拟专用网络实践
  12. (Django开发)免费HTML模板资源集合
  13. FPGA数字鉴相鉴频器的开发记录
  14. 香港TVB40年武侠情侣
  15. 项目实训(一)基于unity的2D多人乱斗闯关游戏设计与开发 unity的下载及了解
  16. 开学送礼最佳选择,有名的蓝牙耳机推荐
  17. 关于我转行嵌入式的那些事
  18. 【实战】前端必会 —— 微信小程序引入背景图
  19. 【路径规划】基于matlab DWA动态避障路径规划【含Matlab源码 2356期】
  20. 计算机科学技术发展现状及前景展望,计算机科学技术的现状及其发展前景分析原稿...

热门文章

  1. Windows10 右键“打开文件所在位置”找不到应用程序
  2. matlab调用kmeans_使用 K 均值聚类实现基于颜色的分割
  3. 基于Ext JS的模块化应用框架搭建及开发
  4. Ext JS的模块化开发(Package)
  5. 年夜饭之 --- 蒜蓉粉丝蒸扇贝
  6. 数据库oracle修改属性列,Oracle修改表结构
  7. python动力学建模与仿真_PyMC3中的简单动力学模型
  8. Keil MDK下载程序时的相关设置
  9. Spring Cloud 相关配置信息说明
  10. CSS中的contenteditable属性