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

概述:

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

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

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

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

  1. - (BOOL)shouldAutorotate;
  2. - (NSUInteger)supportedInterfaceOrientations;

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

1.

2.

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

IOS6自动旋转设置:

IOS6里面,控制某个viewController旋转并不是像IOS5或者IOS4一样在这个viewController里面重写上面那2个方法,而是需要在这个viewController的rootViewController(根视图控制器)里面重写,怎么解释呢?就是最前面的那个viewController,直接跟self.window接触的那个controller,比如以下代码:
  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方法,比如:

  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,一般的写法都是
  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里面的实现方法改为以下方式:
  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有被拒的风险:

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

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

  1. self.view.transform = CGAffineTransformMakeRotation(M_PI/2)

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

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

IOS6手动旋转:

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

  1. - (BOOL)shouldAutorotate{
  2. return NO;
  3. }

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

  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

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

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

  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代替或者直接写死数字!

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

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

  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. AVL树平衡旋转详解

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

  2. android屏幕适配详解

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

  3. Android apk签名详解——AS签名、获取签名信息、系统签名、命令行签名

    Apk签名,每一个Android开发者都不陌生.它就是对我们的apk加了一个校验参数,防止apk被掉包.一开始做Android开发,就接触到了apk签名:后来在微信开放平台.高德地图等平台注册时,需要 ...

  4. Android屏幕适应详解(二)

    android应用自适应多分辨率的解决方法 1. 首先是建立多个layout文件夹(drawable也一样).  在res目录下建立多个layout文件夹,文件夹名称为layout-800x480等. ...

  5. Android屏幕适应详解(一)

    一.关于布局适配 1.不要使用绝对布局 2.尽量使用match_parent 而不是fill_parent . 3.能够使用权重的地方尽量使用权重(android:layout_weight) 4.如 ...

  6. Android屏幕分辨率详解(VGA、HVGA、QVGA、WVGA、WQVGA)

    这些术语都是指屏幕的分辨率. VGA:Video Graphics Array,即:显示绘图矩阵,相当于640×480 像素: HVGA:Half-size VGA:即:VGA的一半,分辨率为480× ...

  7. Android屏幕计量单位详解

    1.px (pixels)(像素):是屏幕的物理像素点,与密度相关,密度大了,单位面积上的px会比较多.通常不推荐使用这个. 2.dip或dp(与密度无关的像素):一个基于density的抽象单位,这 ...

  8. 手机屏幕材质详解(TFT,TPS,OLED,AMOLED等)

    手机屏幕概括起来就是两种,一个是LCD,一个是OLED屏幕,这两个是屏幕显示技术的两大基础. 一 . LCD:Liquid Crystal Display,这是一种介于固态和液态之间的物质,称为液晶技 ...

  9. 详解在Visual Studio中使用git版本系统[转]

    这篇教程的预期,是希望没有任何版本使用基础的新手也可以掌握,所以细节较多,不当之处,欢迎指正. 一 .安装 git 开发工具 如果要使用 git 进行版本管理,其实使用 git 命令行工具就完全足够了 ...

随机推荐

  1. freeCodeCamp:Title Case a Sentence

    确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. /*思路 将字符串转为小写.toLowerCase() 分割字符串以单词形式组成数组myarr 确保数组中的 ...

  2. freeCodeCamp:Check for Palindromes

    如果给定的字符串是回文,返回true,反之,返回false. 如果一个字符串忽略标点符号.大小写和空格,正着读和反着读一模一样,那么这个字符串就是palindrome(回文). 注意你需要去掉字符串多 ...

  3. 《UML大战需求分析》阅读笔记3

    流程分析利器--活动图 活动图属于行为建模的一种.用来分析的不是系统中的某个属性,而是系统或是其中某个部分的活动.结构建模表达的是静态内容,行为建模表达的是动态内容. 在介绍活动图之前,先介绍一下行为 ...

  4. 如何制作u盘启动盘

    1,下载windows系统            许多人下载windows时会出现各种版本,我推荐在这里下载 你用上面的地址进行下载,一般用迅雷吧.下载结束你就有了镜像文件了. 2,下载安装碟软通 那 ...

  5. [新概念51单片机C语言教程·郭天祥] 1、 基础知识必备

    目录: 单片机的大致介绍         1-1.通俗定义         1-2.51系列产品         1-3.标号意思         1-4.引脚介绍         1-5.用C语言开 ...

  6. [游戏模版21] Win32 物理引擎 能量守恒

    >_<:Only a little change in the function of MyPaint(...),besides the initial value have some c ...

  7. vscode中启动浏览器的tasks.json

    {    // See https://go.microsoft.com/fwlink/?LinkId=733558    // for the documentation about the tas ...

  8. js函数的调用问题

    1.js函数的调用方式有三种.请问以下“二”处的几行代码有什么猫腻? //一 事件调用 btn.onclick=fn; //二 直接调用(window调用) fn(); //自上而下解析到这一行的时候 ...

  9. atitit. groupby linq的实现(1)-----linq框架选型 java .net php

    atitit.  groupby linq的实现(1)-----linq框架选型 java .net php 实现方式有如下 1. Dsl/ Java8 Streams AP ,对象化的查询api , ...

  10. paip.抓取网页内容--java php python

    paip.抓取网页内容--java php python.txt 作者Attilax  艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...