http://www.molotang.com/articles/1530.html

接着上篇写的触摸事件,这次借机会整理下iOS横屏和竖屏的翻转方向支持,即InterfaceOrientation相关的内容

最近做一个页面,最初并没有太多考虑orientation的情况,当其嵌入到一个在iPad上使用横屏(Landscape)的应用中,就会只显示在屏幕的左面,而且貌似还没显示全,这个……很丑!发自内心地觉得这么做对不起苹果的设计理念!对不起乔老爷子。。。

改!说到该就要了解苹果开发中对iOS应用的横屏(Landscape)和竖屏(Portrait)的支持情况。

0. 应用级别的配置

大家(特指有iOS开发经验的人)应该都知道Xcode Project的工程配置General页签中有那么四个图(或者4个checkbox),标识对四种interfaceOrientation的支持。分别为Portrait、PortraitUpsideDown、LandscapeLeft和LandscapeRight。

对应的,在Xcode Project工程配置的Info页,实际上就是Info.plist中,有对4种Orientation的记录项。

这两者是一样的。

1. Window级别的控制

在iOS6.0之后,UIApplicationDelegate中多了一个方法声明:

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

就是对于特定的application和特定的window,我们需要支持哪些interfaceOrientation,这是可以通过实现这个方法定制的。

返回值是一个无符号整数,实际上是可以使用定义好的枚举值:

1
2
3
4
5
6
7
8
9
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};

对于UIApplicationDelegate的这个方法声明,大多数情况下application就是当前的application,而window通常也只有一个。所以基本上通过window对横屏竖屏interfaceOrientation的控制相当于全局的。

2. Controller层面的控制

老版本的iOS有这样一个方法:

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

即定制是否可以旋转到特定的interfaceOrientation。

而在iOS6之后,推出了2个新的方法来完成这个任务:

1
2
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);
- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);

可以看得出来,两个和在一起就是原来任务的完成过程。其中,大概的判断方式是,先执行前者,判断是否可以旋转,如果为YES,则根据是否支持特定的interfaceOrientation再做决断。

3. 使得特定ViewController坚持特定的interfaceOrientation

iOS6之后还提供了这样一个方法,可以让你的Controller倔强第坚持某个特定的interfaceOrientation:

1
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);

这就叫坚持,呵呵!
当然,使用这个方法是有前提的,就是当前ViewController是通过全屏的Presentation方式展现出来的。

这里使用的是另外一套枚举量,可以去UIApplication.h中查看定义。

4. 当前屏幕方向interfaceOrientation的获取

有3种方式可以获取到“当前interfaceOrientation”:

  • controller.interfaceOrientation,获取特定controller的方向
  • [[UIApplication sharedApplication] statusBarOrientation] 获取状态条相关的方向
  • [[UIDevice currentDevice] orientation] 获取当前设备的方向

具体区别,可参见StackOverflow的问答:

http://stackoverflow.com/questions/7968451/different-ways-of-getting-current-interface-orientation

5. 容器Controller的支持

上面把interfaceOrientation方向的获取和支持配置都说了,看起来没什么问题了。有没有什么特殊情况?

当你使用TabbarController和NavigationController按照如上做法使用的时候就会有些头疼。

办法不是没有,比较通俗的一种就是——继承实现。

(补充:iOS7之后可以通过delegate对此进行控制)

关于iOS interface orientation屏幕方向的内容就整理到此,欢迎各位看官发言。

iOS的横屏(Landscape)与竖屏(Portrait)InterfaceOrientation的更多相关文章

  1. iOS强制横屏或强制竖屏

    原文链接 https://www.jianshu.com/p/d6cb54d2eaa1 亲测第二种我这边是阔以滴 第一种解决方案(不推荐,直接跳过看第二种解决方案): //强制转屏 - (void)i ...

  2. Android开发中如何强制横屏和强制竖屏设置

    Android开发中如何强制横屏和强制竖屏设置 强制横屏设置: 按照下面代码示例修改Activity的onResume方法 @Override protected void onResume() { ...

  3. [JS代码]如何判断ipad或者iphone是否为横屏或者竖屏 - portrait或者landscape

    //判断横屏或者竖屏 function orient() { //alert('gete'); if (window.orientation == 0 || window.orientation == ...

  4. 二维码识别项目zxing横屏改为竖屏

    第1步: 在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改: android:screenOrientation="por ...

  5. iOS 工程默认只允许竖屏,在单独界面进行横竖转换,屏幕旋转

    只含有 .关于横竖屏的代码 #import "InspectionReportViewController.h" #define SCREEN_WIDTH ([UIScreen m ...

  6. 单个ViewController支持横屏,其他全竖屏方法-b

    如果项目中用了navigationViewController, 那么就应该新建一个uinavigationViewController的子类,然后在这个类里面写上下面的代码,在使用的时候就用自定义的 ...

  7. js判断手机浏览器是横屏or竖屏

    移动端的浏览器一般都支持window.orientation这个参数,通过这个参数可以判断出手机是处在横屏还是竖屏状态. 从而根据实际需求而执行相应的程序.通过添加监听事件onorientationc ...

  8. 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏

    function orient() {if (window.orientation == 90 || window.orientation == -90) {//ipad.iphone竖屏:Andri ...

  9. JQuery 判断IPad、IPhone、Android是横屏还是竖屏(Window.Orientation实现)

    在ipad.iphone网页开发中,我们很可能需要判断是横屏或者竖屏.下面就来介绍如何用 jQuery 判断iPad.iPhone.Android是横屏还是竖屏的方法. 代码如下: function ...

随机推荐

  1. UIApplicationDelegate 协议 浅析

    @protocol UIApplicationDelegate<NSObject> @optional - (void)applicationDidFinishLaunching:(UIA ...

  2. 使用devcon禁用启用网卡

    系统平台:win2003 情况描述: 机器上装有两块网卡,8136和8139,网卡A使用静态IP,连接内部办公网,网卡B使用DHCP,连接互联网.切换两个网络时,需要先禁用一个网卡,启用另一个网卡.来 ...

  3. TForm类有关属性简介

    http://www.cnblogs.com/pchmonster/archive/2012/01/02/2310377.html

  4. POJ 2773 Happy 2006(欧几里德算法)

    题意:给出一个数m,让我们找到第k个与m互质的数. 方法:这题有两种方法,一种是欧拉函数+容斥原理,但代码量较大,另一种办法是欧几里德算法,比较容易理解,但是效率很低. 我这里使用欧几里德算法,欧几里 ...

  5. circularprogressbar/smoothprogressbar开源视图使用学习

    github地址:https://github.com/castorflex/SmoothProgressBar 多彩圆形进度条和多彩水平进度条 colors.xml 定义变化的颜色内容,用gplus ...

  6. ubuntu 下重装mysql若干问题

    最近由于种种原因需要重装mysql.打开终端开始卸载 sudo apt-get remove mysql-comm sudo apt-get auto remove 重新开始安装mysql sudo ...

  7. 在MFC对话框中添加状态栏

    如果我们想实现在MFC对话框中添加状态栏显示,如何例如分状态栏为两列,第一列显示鼠标的当前位置,第二列显示当前的时间,(如上图). 1. 首先,打开在资源视图的String Table并添加两个ID: ...

  8. 使用NGUI进行类似Button的操作

    下面例子以Label和Sprite为例,进行说明如何在NGUI中对控件添加单击响应的事件方法. 首先在UIRoot下添加Label控件,然后Scene场景中右键Label-->Attach--& ...

  9. 快速部署Python应用:Nginx+uWSGI配置详解

    在PHP里,最方便的就是deployment了,只要把php文件丢到支持PHP的路径里面,然后访问那个路径就能使用了:无论给主机添加多少PHP应用,只要把目录改好就没你的事了,完全不用关心php-cg ...

  10. Java 错误:找不到或无法加载主类

    环境变量的配置有问题,你改改吧:HOME是 D:\Java\jdk1.8.0_11 Path是 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin CLASSPATH是 .;%JA ...