iOS 横竖屏适配
关于横竖屏适配 也没做过,今天读别人的源码,遇到了。为了了解清楚,就系统的学习一下。
一 横竖屏方向枚举
关于横竖屏一共有三种枚举 UIInterfaceOrientation UIInterfaceOrientationMask UIDeviceOrientation。
1.1 UIInterfaceOrientation与UIDeviceOrientation
为什么这两个放在一起说,好吧,你看看下面这个枚举定义:
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;
对于iOS设备来讲,屏幕状态由以上五种状态。上下翻转还是很好区分的,左右旋转可能就不是很好区分。
如果我们仔细看枚举值的话 会发现一些问题
在处于竖屏和上下翻转的状态下这两个枚举值是一样的,而处于横屏时这两个枚举值刚好相反
所以在有时你发现跟你预期的翻转方向不一样的时候,可能你用错了枚举。
UIDeviceOrientation 是设备的当前所处的方向,而且事实上他有六个值
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down
} __TVOS_PROHIBITED;
分别对应iPhone未知方向,竖直,上下反转,向左旋转,向右旋转,屏幕朝上,屏幕朝下 那么如何区分左右呢
UIDeviceOrientationLandscapeLeft,home键在右侧
UIDeviceOrientationLandscapeRight,home键在左侧
所以,UIDevice顾名思义,事实上是用来判断设备方向的。
UIInterfaceOrientation 即当前页面的方向。
在设备进行横屏旋转的时候,为了横屏时上下不翻转,所以当Device处于Left时,界面应该是Right旋转。这样才不会使横屏时内容上下翻转。
所以对于横竖屏适配,使用的枚举大家一定要看好,使用UIInterfaceOrientation。不要搞反。
1.2 UIInterfaceOrientationMask
这是iOS6之后新增的一组枚举值
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = ( << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = ( << UIInterfaceOrientationLandscapeLeft),// home 键 在右
UIInterfaceOrientationMaskLandscapeRight = ( << UIInterfaceOrientationLandscapeRight),//home键 在左
UIInterfaceOrientationMaskPortraitUpsideDown = ( << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;
事实上我们在横竖屏适配时,最常用的是这个枚举。这个枚举详细的列举了各种你需要的情况
二 开启横竖屏的权限

可以看到这种勾选方式允许你进行四个方向的配置,并且这种勾选方式会直接在你的项目plist文件中添加

但是由于在这里配置是对项目启动时lanuch界面产生影响,而往往我们又没有对lanuch进行横竖屏适配,所以在这个时候我们就需要使用第二种方式进行配置。
在项目中的AppDelegate文件中进行配置。
#pragma mark - InterfaceOrientation //应用支持的方向
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
搭配UIInterfaceOrientationMask使用,你可以很方便的让你项目开启你所需要的横竖屏权限和限制条件。
三 在VC中如何控制横竖屏
我们都知道MVC架构,那么显而易见,在我们开启了项目的横竖屏的限制之后,需要在ViewController进行相应的配置,才能真正实现横竖屏。
开启横竖屏,我们需要在VC中添加如下代码
// 设备支持方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
// 默认方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait; // 或者其他值 balabala~
}
而对于横竖屏,手机端一般有两种情况,一种是手机没有开启横竖屏锁定,用户将手机横屏时触发的。对于第一种情况,我们只需要在VC中添加:
// 开启自动转屏
- (BOOL)shouldAutorotate {
return YES;
}
另一种是我们在项目中的某些条件下强行让屏幕横屏,例如大图预览,视频播放,等等。而对于这种情况,我们可以使用下面
iOS 横竖屏适配的更多相关文章
- iOS 横竖屏适配 笔记
研究消息转发机制 已经一周多了,但是 还是没整理出博客, 还是先写一个 项目中遇到的 横竖屏适配问题. // 开启自动转屏 - (BOOL)shouldAutorotate { return YES; ...
- 48 (OC)* 适配iPad和iPhone、以及横竖屏适配。
一:核心方法 1.三个方法 1.1:开始就会触发 - (void)viewWillLayoutSubviews; 1.2:开始就会触发 - (void)viewDidLayoutSubviews; 1 ...
- css横竖屏适配
Css做到横竖屏适配:定义两个样式 { @media screen and (orientation: portrait){ Css[竖向定义样式] } @media screen and (orie ...
- iOS 横竖屏切换(应对特殊需求)
iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...
- iOS横竖屏切换的一些坑(持续更新)
最近在做视频类的App,遇到视频滚动播放的坑,紧接着就是横竖屏问题.之前太过天真不想做横竖屏配置.只是想旋转视频View,但是分享什么的包括AlertView还是竖屏样式,项目着急上线(1周提交一次也 ...
- iOS 横竖屏切换解决方案
iOS要实现横竖屏切换很简单,不需要使用任何第三方,只需要实现几个方法就可以了. 1.设置系统支持横竖屏[General]->[Targets]-> [Deployment info]-& ...
- (一〇八)iPad开发之横竖屏适配
在iPad开发中,横竖屏的视图常常是不同的,例如侧边栏Dock,在横屏时用于屏幕较宽,可以展示足够多的内容,每个按钮都可以展示出标题:而竖屏时Dock应该比较窄,只显示图标不现实按钮标题. iPad比 ...
- ios 横竖屏通知
屏幕切换时,会发送一个通知.只要注册一个通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(do ...
- IOS横竖屏控制与事件处理
公司App里面有个需求,即所有界面都是竖屏,且不允许横屏切换,唯独有一个图表界面允许横屏.那么,根据此需求处理如下: 首先,确保App本身应该允许转屏切换: 再次,我的App里面都是走UINaviga ...
随机推荐
- iOS之基于FreeStreamer的简单音乐播放器(模仿QQ音乐)
代码地址如下:http://www.demodashi.com/demo/11944.html 天道酬勤 前言 作为一名iOS开发者,每当使用APP的时候,总难免会情不自禁的去想想,这个怎么做的?该怎 ...
- How to manually remove an infected file from your computer
http://blog.csdn.net/pipisorry/article/details/41258577 How to manually remove an infected file from ...
- (转)NSString to string(支持中文)
NSString to string const char* destDir = [filepath UTF8String]; string a=destDir; string to NSString ...
- Verilog HDL test bench 문법에 관한
16bit ripple carry adder test bench `timescale 1ns/1ns module testbench2; reg [15:0] a, [15:0] b, c_ ...
- ubuntu 12.04 install gcc 4.8
http://askubuntu.com/questions/271388/how-to-install-gcc-4-8-in-ubuntu-12-04-from-the-terminal sudo ...
- position与float属性的使用
1.使用float时,可以说是用于布局,取值主要有left.right.none.就是将该块的元素浮动起来,在浏览器默认的情况下,该元素的位置是为空的, 即脱离了文档流而存在,如果有其他元素,那么这些 ...
- tomcat各目录下的作用
1.tomcat的主目录下的文件 bin目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结尾的(linux命令),另一类是以.bat结尾的(windows命令). 重要: 很多环 ...
- 分布式服务框架 Zookeeper(二)官方介绍
ZooKeeper:为分布式应用而生的分布式协调服务 ZooKeeper是一个为分布式应用而设计的分布式的.开源的协调服务.它提供了一套简单的原语,分布式应用利用这套原语可以实现更高层的服务,比如一致 ...
- Consul实现原理系列文章1: 用Raft来实现分布式一致性
工作中用到了Consul来做服务发现,之后一段时间里,我会陆续发一些文章来讲述Consul实现原理.在前一篇文章中,我介绍了Raft算法.这篇文章会讲讲Consul是如何使用Raft算法来实现分布式一 ...
- Spring3.2.6 + hibernate4.2.8 + hibernate-generic-dao1.2.0
n多方法都不成功,最后在hibernate-generic-dao官网上的例子hibernate-maven-web(使用其jar包及配置,并将hibernate更新为4.2.8,加入ehcache及 ...