iOS6的旋屏控制技巧

在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

但是在iOS6中,这个方法被废弃了,使用无效。

shouldAutorotateToInterfaceOrientation:

Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in iOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)

实践后会发现,通过supportedInterfaceOrientations的单独控制是无法锁定屏幕的。

-(NSUInteger)supportedInterfaceOrientations

{  return UIInterfaceOrientationMaskPortrait;

}

多次实验后总结出控制屏幕旋转支持方向的方法如下:

子类化UINavigationController,增加方法

- (BOOL)shouldAutorotate

{

return self.topViewController.shouldAutorotate;

}

- (NSUInteger)supportedInterfaceOrientations

{

return self.topViewController.supportedInterfaceOrientations;

}

并且设定其为程序入口,或指定为 self.window.rootViewController

随后添加自己的view controller,如果想禁止某个view controller的旋屏:(支持全部版本的控制)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

-(BOOL)shouldAutorotate

{

return NO;

}

-(NSUInteger)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskPortrait;

}

如果想又开启某个view controller的全部方向旋屏支持:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

  return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

-(NSUInteger)supportedInterfaceOrientations

{

   return UIInterfaceOrientationMaskAllButUpsideDown;

}

-(BOOL)shouldAutorotate

{

return YES;

}

从而实现了对每个view controller的单独控制。

顺便提一下,如果整个应用所有view controller都不支持旋屏,那么干脆:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{

  return UIInterfaceOrientationMaskPortrait;

}

iOS项目旋转控制的更多相关文章

  1. 开源 iOS 项目分类索引大全 - 待整理

    开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...

  2. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  3. 现有iOS项目集成React Native过程记录

    在<Mac系统下React Native环境搭建>配置了RN的开发环境,然后,本文记录在现有iOS项目集成React Native的过程,官方推荐使用Cocoapods,项目一开始也是使用 ...

  4. iOS项目的本地化处理(多国语言)

    项目的本地化就是:iOS系统在不同语言环境下自动切换语言,从而实现一个app发布到全世界各个国家的AppStore上. 我们不仅仅需要在iOS项目中做本地化处理,在上架iOS APP的时候,也需要做对 ...

  5. 在Xcode 6 beta里编译Cocos2d-x iOS项目时失败

    转载 在Xcode 6 beta里编译Cocos2d-x iOS项目时可能会失败,提示如下错误: Undefined symbols for architecture i386: "_fwr ...

  6. phonegap创建的ios项目推送消息出现闪退现象

    使用phonegap创建的ios项目,推送消息时,当程序在前台运行或者在后台运行状态下,推送消息过来,可以解析并且跳转: 但是在程序从后台退出的状态下,当消息推送过来的时候,点击通知栏,打开程序,程序 ...

  7. MVVM 模式下iOS项目目录结构详细说明

    ➠更多技术干货请戳:听云博客 我们在做项目的时候,会经常用到各种设计模式,最常见的要数 MVC (模型,视图,控制器)了.但是,今天我们要说的是另一种设计模式——MVVM. 所以 MVVM 到底是什么 ...

  8. WinObjc - 使用iOS项目生成通用Windows应用

    Github上一周年的WinObjc项目最近发布了预览版本,终于等到了这一天.WinObjc项目就是Build 2015大会上微软宣布的Project IslandWood项目,致力于将iOS应用快速 ...

  9. iOS 项目中用到的一些开源库和第三方组件

    iOS 项目中用到的一些 iOS 开源库和第三方组件 分享一下我目前所在公司 iOS 项目中用到的一些 iOS 开源库和第三方组件, 感谢开源, 减少了我们的劳动力, 节约了我们大量的时间, 让我们有 ...

随机推荐

  1. Android根据文件路径加载指定文件

    Android根据指定的文件路径加载指定文件格式(图片格式 png, gif,jpg jpeg)的文件相关信息的列表. 如图: 代码: public class Util { /**** * 计算文件 ...

  2. python 识别图片验证码报IOError

    说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...

  3. ios UILabel在storyBoard或xib中如何在每行文字不显示完就换行

    大家知道怎么用代码让label中的文字换行,只需要 label.numberOfLines = 0; label.text = @"这是第一行啦啦啦啦,\n这是第二行啦啦,\n这是第三行&q ...

  4. js获取get方式传递的参数

    String.prototype.GetValue= function(parm) { var reg = new RegExp("(^|&)"+ parm +" ...

  5. oracle java SE

    http://www.oracle.com/technetwork/java/javase/downloads/index.html

  6. gulp LiveReload middleware

    用yo搭建的angular项目,用gulp自动化构建. 自动化构建主要的功能大致有: 1. 文件压缩 2. 文件重命名 3. 文件合并 4. css,js文件自动引入到html 5. 自动刷新 ... ...

  7. git clone google代码库

    git clone  https://chromium.googlesource.com/chromium/src 发现有将近7G,但是速度太慢,老是失败,提示信息先后是"The remot ...

  8. django TEMPLATES

    ?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dic ...

  9. 下拉tableView实现类似微信中带图的灰色背景

    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(, -, ScreenWidth, )]; UIImageView *iconIma ...

  10. VBA学习之关于数据透视表的应用

    工作中很多地方需要同时处理多个数据表,而且用数据透视表进行排版,排序,计算字段,一个一个的做非常累,这里给出批量处理的方法. 学习VBA之前最好懂一点点VB的基础知识,因为里面的很多语法问题都是由VB ...