当前位置: 首页 > news >正文

iOS9横竖屏设置的处理方法

在一般的视频类APP播放的时候都会支持横屏,这样做的好处就是便于观看。你的项目中支持横屏吗?我们一起了解一下,在iOS9中横竖屏设置的处理方法吧!

支持横竖屏配置

在iOS6以后,如果APP需要支持横屏,需要在xcode设置中General里面进行勾选配置:

orientation1

配置完成之后,我们可以看一下Info.plist里面的Supported interface orientations选项也相应的改变了。如下图:

orientation2

当然,我们也可以直接在Info.plist进行配置。

支持横竖屏方法

在iOS6之前我们可以直接用这个方法进行配置:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED; 

在iOS6之后,这个方法被NS_DEPRECATED_IOS,也就是废弃掉了。废弃了这个方法,苹果相应的也给出了新的方法来代替:

// New Autorotation support.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

我们可以看到iOS6之前是一个方法,在iOS6之后变成两个方法了,一个是是否旋转的方法,一个是支持的方向的方法。

实例一:

假设:我们ViewController是直接加载window的self.window.rootViewController上面的。代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    return YES;
}

如果我们要是想支持上面General里勾选的方向(竖屏、横屏向左已经横屏向右)该如何实现呢?首先,我们应该设置让他支持旋转,然后在设置支持的方向。代码如下:

 1 //支持旋转
 2 -(BOOL)shouldAutorotate{
 3    return YES;
 4 }
 5 //支持的方向
 6 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
 7     return UIInterfaceOrientationMaskAllButUpsideDown;
 8 }
 9 其中UIInterfaceOrientationMask是一个枚举:
10 
11 typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
12     UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
13     UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
14     UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
15     UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
16     UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
17     UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
18     UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
19 } __TVOS_PROHIBITED;

可以根据自己的需求来选择。上面我们说了假设这个条件,如果rootViewController上导航,我们直接在ViewController里面设置,这个方法就不灵了。(大家可以自己测试一下)

实例二:

为什么是导航上面的方法就不灵了呢?原因很简单,我们没有设置导航支持的方向。别忘了UINavigationController也是UIViewController的子类。需要受到同样的待遇的。

如何设置呢?我们可以创建一个UINavigationController的子类,假设叫GGPublicNavigationViewController。然后,我们在GGPublicNavigationViewController.m文件里面也实现着两个方法:

1 //支持旋转
2 -(BOOL)shouldAutorotate{
3    return YES;
4 }
5 //支持的方向
6 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
7     return UIInterfaceOrientationMaskAllButUpsideDown;
8 }

这样设置之后,即使我们push进去的UIViewController没有实现上面的连个方法,也是可以支持横屏的。也就是说,我们push的所有都支持横屏。这个做法是不是很暴力!

实例三:

有些童鞋会问了,如何控制每个界面支持的方向呢?这也是可以办到的,在GGPublicNavigationViewController不能写死支持哪个。我们可以这么写:

1 -(BOOL)shouldAutorotate{
2     return [self.topViewController shouldAutorotate];
3 }
4 //支持的方向
5 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
6     return [self.topViewController supportedInterfaceOrientations];;
7 }

self.topViewController是当前导航显示的UIViewController,这样就可以控制每个UIViewController所支持的方向啦!

 

转载于:https://www.cnblogs.com/fengmin/p/5421515.html

相关文章:

  • spark 性能优化
  • 第一阶段冲刺个人博客08
  • 【代码笔记】iOS-轮询弹出框
  • 圆角矩形“RoundRectShape”使用详解
  • Javascript异步编程的4种方法
  • [APIO2015]巴厘岛的雕塑
  • HTTP原理
  • javascript中利用柯里化函数实现bind方法
  • theano和keras使用过程中遇到的一些问题记录
  • 20145228《Java程序设计》第九周学习总结
  • Atitit.redis操作总结
  • Java数组技巧攻略
  • Java编程思想读书笔记之内部类
  • 2、变量var关键字
  • 基于CkEditor实现.net在线开发之路(5)列表页面开发
  • Golang-长连接-状态推送
  • gops —— Go 程序诊断分析工具
  • JavaScript设计模式与开发实践系列之策略模式
  • Mithril.js 入门介绍
  • Mysql优化
  • oldjun 检测网站的经验
  • Python - 闭包Closure
  • tensorflow学习笔记3——MNIST应用篇
  • Vue.js源码(2):初探List Rendering
  • 说说动画卡顿的解决方案
  • 推荐一个React的管理后台框架
  • 微信小程序--------语音识别(前端自己也能玩)
  • 学习笔记TF060:图像语音结合,看图说话
  • 运行时添加log4j2的appender
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • const的用法,特别是用在函数前面与后面的区别
  • 阿里云API、SDK和CLI应用实践方案
  • 阿里云服务器如何修改远程端口?
  • $(selector).each()和$.each()的区别
  • (007)XHTML文档之标题——h1~h6
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (BFS)hdoj2377-Bus Pass
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (层次遍历)104. 二叉树的最大深度
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (理论篇)httpmoudle和httphandler一览
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • .a文件和.so文件
  • .bat批处理(六):替换字符串中匹配的子串
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net core Swagger 过滤部分Api
  • .net core webapi 部署iis_一键部署VS插件:让.NET开发者更幸福
  • .NET性能优化(文摘)
  • .project文件
  • [ vulhub漏洞复现篇 ] AppWeb认证绕过漏洞(CVE-2018-8715)
  • [Android Pro] listView和GridView的item设置的高度和宽度不起作用
  • [Android View] 可绘制形状 (Shape Xml)
  • [c++] C++多态(虚函数和虚继承)