参考文章:http://www.tuicool.com/articles/e2q6zi

一般的应用,只会支持竖屏正方向一个方向,支持多个屏幕方向的应用还是比较少的。 当时也没搞明白,所以直接就设置了正方向一个方向。今天看书无意中看到这个旋转部分才搞懂,原来是IOS 6.0前后控制的方法不一样了,怪不得当时不起作用,现在记录下:

系统屏幕方向枚举

通过查看文档,用于控制系统屏幕方向的枚举如下:

// iOS 6 之前用于控制屏幕方向的枚举
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation; // iOS 6 及之后版本用于控制屏幕方向的枚举
typedef enum {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
UIInterfaceOrientationMaskLandscapeRight),
} UIInterfaceOrientationMask;

可以发现:

  • iOS 6 及之后版本使用的 UIInterfaceOrientationMask 类型来控制屏幕屏幕方向,该类型也新增加了几个枚举取值,可用一个枚举取值来代表多个屏幕方向。
  • 四个基本屏幕方向(上、下、左、右)中,UIInterfaceOrientationMask
    = (1 << UIInterfaceOrientation)
     ,所以,如果你的应用中需要动态的将 UIInterfaceOrientation 类型转换成 UIInterfaceOrientationMask 类型的话,只需做一下上面的转换即可,不需要通过 switch 来判断再转换。

怎么控制屏幕方向

在 iOS 的应用中,有多种方式可以控制界面的屏幕方向,有全局的,有针对 UIWindow 中界面的控制,也有针对单个界面。

单个界面控制

iOS 6之前

在 iOS 6 之前,单个界面的屏幕方向控制,都使用 UIViewController 类中的这个方法:

// 是否支持旋转到某个屏幕方向,此处是支持横向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) |
(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}

或者这样写

// 是否支持旋转到某个屏幕方向,此处是支持横向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

默认情况下,此方法只有参数为 UIInterfaceOrientationPortrait 时,返回值才为真,即默认只支持竖屏向上。上面的例子中,表示支持横屏向右及横屏向左两个方向。

iOS 6及之后的版本

在 iOS 6 及之后的版本,单个界面的屏幕方向控制,要使用 UIViewController 在 iOS 6.0 中新增加的两个方法:

// 是否支持转屏
- (BOOL)shouldAutorotate
{
return YES;
} // 支持的屏幕方向,此处可直接返回 UIInterfaceOrientationMask 类型
// 也可以返回多个 UIInterfaceOrientationMask 取或运算后的值,多个值时,用|,注意不是||
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}

其中 - supportedInterfaceOrientations 方法在
iPad 中默认取值为 UIInterfaceOrientationMaskAll,即默认支持所有屏幕方向;而 iPhone 跟 iPod Touch 的默认取值为 UIInterfaceOrientationMaskAllButUpsideDown,即支持除竖屏向下以外的三个方向。 

在设备屏幕旋转时,系统会调用 - shouldAutorotate 方法检查当前界面是否支持旋转,只有-
shouldAutorotate
 返回 YES 的时候, -
supportedInterfaceOrientations
 方法才会被调用,以确定是否需要旋转界面。

UIWindow中的界面控制(iOS 6及以后版本才有效)

在 iOS 6 中,UIApplicationDelegate 协议中添加了一个可以指定 UIWindow 中的界面的屏幕方向的方法:

- (NSUInteger)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskLandscape;
}

此方法的默认值为 Info.plist 中配置的 Supported interface orientations 项的值。 

一般我们都不会创建其他的 UIWindow,所以通过这个方法,也可以达到全局控制。

全局控制

在应用的 Info.plist 文件中,有一个 Supported interface orientations 的配置,可以配置整个应用的屏幕方向,如下图: 

此配置其实跟工程中 Target 的 Summary 界面中的 Supported interface orientations 配置是一致的,修改任意一边,另一个边都会同步的修改。

并且,应用在启动时,会使用 Info.plist 中的 Supported interface orientations 项中的第一个值作为启动动画的屏幕方向。按照此处截图的取值,第一个取值为 Portrait(top
home button)
 ,即竖屏反方向,所以此应用在启动时,会使用竖屏反方向显示启动动画。

多种控制共存的规则

  • 一个界面最后支持的屏幕方向,是取(全局控制
    ∩ UIWindow 中的界面控制 ∩ 单个界面控制)
     的交集,如果全局控制支持所有屏幕方向,UIWindow 中的界面控制支持横屏,当个界面中只是支持横屏向右,那么最后界面只会以横屏向右显示,并且不支持旋转到其他的方向。
  • 如果以上三种控制支持的屏幕方向最后的交集为空,iOS 5 跟 iOS 6 的处理有点不同,在 iOS 6 下,甚至会直接抛出 UIApplicationInvalidInterfaceOrientationException 的异常,然后直接崩溃,所以还是要保持这三个值的交集为非空。

iOS 屏幕方向的更多相关文章

  1. ios 屏幕方向的设置

    ref: http://www.cnblogs.com/niit-soft-518/p/5611298.html 实际的项目需求.root是TabBarController,里面有4个navigati ...

  2. iOS屏幕旋转 浅析

    一.两种orientation 了解屏幕旋转首先需要区分两种orientation 1.device orientation 设备的物理方向,由类型UIDeviceOrientation表示,当前设备 ...

  3. 谈谈iOS中的屏幕方向

    众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...

  4. iOS如何用代码控制以不同屏幕方向打开新页面?

    转载:http://blogread.cn/it/article/7765?f=wb#original 代码示例:https://github.com/johnlui/Swift-On-iOS/tre ...

  5. UI: 概述, 启动屏幕, 屏幕方向

    UI 设计概述 启动屏幕(闪屏) 屏幕方向 示例1.UI 设计概述UI/Summary.xaml <Page x:Class="Windows10.UI.Summary" x ...

  6. 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向

    [源码下载] 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向 作者:webabcd 介绍背水一战 Windows 10 之 UI UI 设计概述 启动屏幕(闪屏) 屏 ...

  7. iOS屏幕适配-iOS笔记

    学习目标 1.[了解]屏幕适配的发展史 2.[了解]autoResizing基本用法 3.[掌握]autoLayout 的基本用法 4.[掌握]autoLayout代码实现 5.[理解]sizeCla ...

  8. js监听屏幕方向如何第一次默认不监听

    this.supportOrientation = typeof window.orientation === 'number'; // 检查屏幕方向 checkScreenOrientation() ...

  9. Android中Activity运行时屏幕方向与显示方式详解

    现在我们的手机一般都内置有方向感应器,手机屏幕会根据所处位置自动进行横竖屏切换(前提是未锁定屏幕方向).但有时我们的应用程序仅限在横屏或者竖屏状态下才可以运行,此时我们需要锁定该程序Activity运 ...

随机推荐

  1. BZOJ1187 [HNOI2007]神奇游乐园(插头dp)

    麻麻我会写插头dp了! 推荐陈丹琦论文:https://wenku.baidu.com/view/3e90d32b453610661ed9f4bd.html 破题调一年 #include <cs ...

  2. PTA 社交网络图中结点的“重要性”计算(30 分)

    7-12 社交网络图中结点的“重要性”计算(30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的一种相互 ...

  3. HashSet与TreeSet

    1.TreeSet 是二差树实现的,Treeset中的数据是自动排好序的,不允许放入null值 2.HashSet 是哈希表实现的,HashSet中的数据是无序的,可以放入null,但只能放入一个nu ...

  4. BI之SSIS入门最新版Visual Studio调试技巧

    简介 最近公司业务需要用到BI SSIS,SSIS是什么?"SSIS是Microsoft SQL Server Integration Services的简称,是生成高性能数据集成解决方案( ...

  5. sqlserver 判断字段是否为空字符串或者null

    isnull(f.mzm,'')<>'' 不为null且不为‘’ not(f.mzm is null) 不为null

  6. lvs+keepalive实现双主模式(采用DR),同时实现TCP和UDP检测实现非web端的负载均衡,同时实现跨网段的通讯

    因为公司领导需要,需要把lvs备机也使用上,故! 使用双主,相互是主的同时也相互是备机.本人用nat测试发现RS无法实现负载均衡,故采用DR模式来实现非web端的负载均衡 lvs1: DIP 10.6 ...

  7. jspacker压缩及解压缩研究(js eval)

    起因: 在研究爬虫的时候发现很多网站都出现了同一种方式的js混淆,并且名字都是pde.js,怀疑是使用了同一款混淆工具,所以研究一下. 这款工具叫JS Packer,并不是专门的混淆工具,而是一款js ...

  8. Winform DevExpress控件库(一) DevExpress控件库的安装与新建第一个DevExpress项目

    前言:因为这段时间要接触到DevExpress控件库,而我本身甚至对winform的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...

  9. 计算机网络之文件传送协议FTP

    FTP 文件传送协议FTP(File Transfer Protocol)是因特网上使用最广泛的文件传送协议. FTP 提供交互式的访问,允许客户指明文件的类型与格式,并允许文件具有存取权限.FTP ...

  10. [boost] build boost with intel compiler 16.0.XXX

    Introduction There are few information about how to compile boost with Intel compiler. This article ...