IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
转自 http://blog.csdn.net/zzfsuiye/article/details/8251060
概述:
在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewController支持旋转,只需要重写shouldAutorotateToInterfaceOrientation方法。
但是iOS 6里屏幕旋转改变了很多,之前的 shouldAutorotateToInterfaceOrientation 被列为 DEPRECATED 方法,查看UIViewController.h文件也可以看到:
- // Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
程序将使用如下2个方法来代替:
- - (BOOL)shouldAutorotate;
- - (NSUInteger)supportedInterfaceOrientations;
除了重写这个2个方法,IOS6里面要旋转还有一些需要注意的地方,下面会细述。另外还有一个硬性条件,需要在Info.plist文件里面添加程序支持的所有方向,可以通过以下2种方式添加
1.
2.
另外要兼容IOS6之前的系统,要保留原来的 shouldAutorotateToInterfaceOrientation 方法,还有那些 willRotateToInterfaceOrientation 等方法。
IOS6自动旋转设置:
- UIViewController *viewCtrl = [[UIViewController alloc] init];
- UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
- if ([window respondsToSelector:@selector(setRootViewController:)]) {
- self.window.rootViewController = navCtrl;
- } else {
- [self.window addSubview:navCtrl.view];
- }
如果需要设置viewCtrl的旋转,那么不能在UIViewController里面重写shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面设置,又因为UINavigationController是系统控件,所以这里需要新建一个UINavigationController的子navigationController的子类,然后在里面实现shouldAutorotate和supportedInterfaceOrientations方法,比如:
- -(NSUInteger)supportedInterfaceOrientations{
- return UIInterfaceOrientationMaskAllButUpsideDown;
- }
- - (BOOL)shouldAutorotate{
- return YES;
- }
- UIViewController *viewCtrl2 = [[UIVewController alloc] init];
- [self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];
- - (BOOL)shouldAutorotate
- {
- return self.topViewController.shouldAutorotate;
- }
- - (NSUInteger)supportedInterfaceOrientations
- {
- return self.topViewController.supportedInterfaceOrientations;
- }
全部调用self.topViewController,就是返回当前呈现出来的viewController里面的设置,然后在viewCtrl、viewCtrl2等等这些viewController里面重写shouldAutorotate和supportedInterfaceOrientations,以方便设置每个viewController的旋转
IOS5、IOS4自动旋转设置
手动旋转
手动旋转也有2种方式,一种是直接设置 UIDevice 的 orientation,但是这种方式不推荐,上传appStore有被拒的风险:
- if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
- [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
- }
第二种是假旋转,并没有改变 UIDevice 的 orientation,而是改变某个view的 transform,利用 CGAffineTransformMakeRotation 来达到目的,比如:
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2)
下面讲解采用第二种方式的各版本手动旋转:
思想是首先设置 statusBarOrientation,然后再改变某个view的方向跟 statusBarOrientation 一致!
IOS6手动旋转:
1. 那既然是旋转,最少也得有2个方向,那么还是少不了上面说的那个硬性条件,先在plist里面设置好所有可能需要旋转的方向。既然是手动旋转,那么就要关闭自动旋转:
- - (BOOL)shouldAutorotate{
- return NO;
- }
2.手动触发某个按钮,调用方法,这个方法的实现如下:
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
- 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
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
- return (interfaceOrientation == [UIApplication sharedApplication].statusBarOrientation);
- }
2.手动触发某个按钮,调用方法,这个方法的实现如下:
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
- 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代替或者直接写死数字!
- -(NSUInteger)supportedInterfaceOrientations{
- return [UIApplication sharedApplication].statusBarOrientation;
- }
3.一般都不建议在程序里面直接调用 UIDeviceOrientation 的方向,而是用 UIInterfaceOrientation,他们之间是不同的!
- UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
- 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之前系统)的更多相关文章
- AVL树平衡旋转详解
AVL树平衡旋转详解 概述 AVL树又叫做平衡二叉树.前言部分我也有说到,AVL树的前提是二叉排序树(BST或叫做二叉查找树).由于在生成BST树的过程中可能会出现线型树结构,比如插入的顺序是:1, ...
- android屏幕适配详解
android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 一.关于布局适配建议 1.不要 ...
- Android apk签名详解——AS签名、获取签名信息、系统签名、命令行签名
Apk签名,每一个Android开发者都不陌生.它就是对我们的apk加了一个校验参数,防止apk被掉包.一开始做Android开发,就接触到了apk签名:后来在微信开放平台.高德地图等平台注册时,需要 ...
- Android屏幕适应详解(二)
android应用自适应多分辨率的解决方法 1. 首先是建立多个layout文件夹(drawable也一样). 在res目录下建立多个layout文件夹,文件夹名称为layout-800x480等. ...
- Android屏幕适应详解(一)
一.关于布局适配 1.不要使用绝对布局 2.尽量使用match_parent 而不是fill_parent . 3.能够使用权重的地方尽量使用权重(android:layout_weight) 4.如 ...
- Android屏幕分辨率详解(VGA、HVGA、QVGA、WVGA、WQVGA)
这些术语都是指屏幕的分辨率. VGA:Video Graphics Array,即:显示绘图矩阵,相当于640×480 像素: HVGA:Half-size VGA:即:VGA的一半,分辨率为480× ...
- Android屏幕计量单位详解
1.px (pixels)(像素):是屏幕的物理像素点,与密度相关,密度大了,单位面积上的px会比较多.通常不推荐使用这个. 2.dip或dp(与密度无关的像素):一个基于density的抽象单位,这 ...
- 手机屏幕材质详解(TFT,TPS,OLED,AMOLED等)
手机屏幕概括起来就是两种,一个是LCD,一个是OLED屏幕,这两个是屏幕显示技术的两大基础. 一 . LCD:Liquid Crystal Display,这是一种介于固态和液态之间的物质,称为液晶技 ...
- 详解在Visual Studio中使用git版本系统[转]
这篇教程的预期,是希望没有任何版本使用基础的新手也可以掌握,所以细节较多,不当之处,欢迎指正. 一 .安装 git 开发工具 如果要使用 git 进行版本管理,其实使用 git 命令行工具就完全足够了 ...
随机推荐
- processing学习笔记
这是从http://funprogramming.org/视频学习过程中做的笔记,没法看视频的话,请FQ 点point(x,y); 线line(x,y,x2,y2); 背景background(x), ...
- Win系统查看系统的几个命令
1. 查看显卡或者显存信息 Win + R dxdiag 2. 查看显卡是独立显卡,或者集成显卡 查看计算机显卡的方法 第一种方法:查看主机连接显示器VGA线连接的接口. 如图:VGA连接线连接在主机 ...
- 解决 yum安装时报错 Error: Protected multilib versions: 报错
系统中缺少一个lib库 libz.so.1文件,使用yum安装会自动找到相关的rpm包,如下命令 # yum -y install libz.so.1 Resolving Dependencies-- ...
- 黑马程序员+Winform基础(上)
黑马程序员+Winform基础 ---------------<a href="http://edu.csdn.net"target="blank"> ...
- linux下vi操作出现E325: ATTENTION的解决方法
#rm ***.***.swp #rm: remove regular file `.Test.java.swp'? y # OK,完成.再用VI打开就可以了.
- 安装时出现 Runtiem error (at 62:321) SWbem Locator:服务不存在,或已被标记为删除 该怎么解决?
这是由wmi服务损坏引起的错误 修复WMI服务损坏的批处理程序 将下列代码复制到一个文本文件中,改名为fixwmi.bat,运行即可.需要一段时间,请大家耐心等候. ================= ...
- hibernate date类型插入数据库时精度只到日期没有时间
由hibernate 的逆向工具从数据库表生成的*.hbm.xml ,对于数据库的date类型生成如下: <property name = "crttime" ...
- 在Android中调用C#写的WebService(附源代码)
由于项目中要使用Android调用C#写的WebService,于是便有了这篇文章.在学习的过程中,发现在C#中直接调用WebService方便得多,直接添加一个引用,便可以直接使用将WebServi ...
- [ucgui] 对话框2——小窗口初始化与消息响应
>_<" 上一节已经说过,创建过得窗口虽然可见,但是它们是以 “空”的形式出现的.这是因为对话框过程函数尚未包含初始化单个元素的代码.小工具的初始值.由它们所引起的行为以及它们之 ...
- 献上两个java小算法
直接上代码: /** * Name: 求数组中元素重复次数对多的数和重复次数 * Description: * 数组中的元素可能会重复,这个方法可以找出重复次数最多的数,同时可以返回重复了多少次. * ...