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

支持横竖屏配置

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

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

当然,我们也可以直接在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里勾选的方向(竖屏、横屏向左已经横屏向右)该如何实现呢?首先,我们应该设置让他支持旋转,然后在设置支持的方向。代码如下:

 //支持旋转
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
其中UIInterfaceOrientationMask是一个枚举: typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = ( << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = ( << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = ( << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = ( << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;

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

实例二:

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

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

 //支持旋转
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}

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

实例三:

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

 -(BOOL)shouldAutorotate{
return [self.topViewController shouldAutorotate];
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];;
}

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

iOS9横竖屏设置的处理方法的更多相关文章

  1. xcode5下cocos2dx横竖屏设置

    我们在开发一款游戏之前一定会考虑的一件事就是,我们的游戏是支持横屏还是竖屏,又或者是横竖屏都支持.那么如何在xcode中对项目进行设置呢?下面我就在xcode5.1.1中利用cocos2dx2.2.3 ...

  2. screen-Orientation 横竖屏设置

    1.xml中设置,这个主要是在AndroidManifest.xml 中查找activity,然后在里面设置属性,如下 <application android:label="@str ...

  3. iTOP4418开发板7寸屏幕Android系统下横竖屏设置

    Android系统屏幕旋转设置 平台: iTOP4418开发板+7寸屏幕 1. Androd4.4源码可以编译成手机模式和平板模式,讯为iTop4418 开发平台的Android系统默认编译为平板模式 ...

  4. react native 添加第三方插件react-native-orientation(横竖屏设置功能 android)

    Installation 1.install  rnpm Run  npm install -g rnpm 2.via rnpm Run rnpm install react-native-orien ...

  5. Android Activity 常用功能设置(全屏、横竖屏等)

    Activity全屏设置 方式1:AndroidManifest.xml <activity android:name="myAcitivty"  android:theme ...

  6. js 判断手机横竖屏的实现方法(不依赖任何其他库)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. Activity 横竖屏切换

    前言 在开发中常要处理横竖屏切换,怎么处理先看生命周期 申明 Activity 横竖屏切换时需要回调两个函数 ,所以在此将这个两个函数暂时看成是Activity 横竖屏切换的生命周期的一部分,这两个函 ...

  8. 【转】Android横竖屏切换问题

    Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...

  9. Android横竖屏切换处理

    Android横竖屏要解决的问题应该就两个: 1.布局问题:2.重新载入问题   一.布局问题: 如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你 ...

随机推荐

  1. erlang学习笔记之基础语法

    字符串是双引号,单引号的是atom元组: 下标从1开始 X = {'test1',2,3,4}. element(1,X). 配合模式匹配,可以给元素项命名,直接不用下标标记元素项 列表增删改查 增加 ...

  2. guake默认快捷键

    toggle guake visibility   切换guake可见 :F12 toggle fullscreen   切换全屏幕 :F11 quit   退出 :Shift+Ctrl+Q new ...

  3. MySQL中间层 Atlas

    Atlas是由 Qihoo 360,  Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bu ...

  4. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇02:内购如何实现》

    2.内购如何实现 内购概述: 游戏内购是指玩家在游戏中,用金钱获取游戏道具的一种方式.开发者从玩家内购之中获得开发游戏的收益,因此大量游戏都有内购模块.下面我们以在手机IOS平台实现跑酷游戏内购为例, ...

  5. PHP算法之二分查找和顺序查找

    一.二分查找 (数组里查找某个元素) /** * 二分查找 (数组里查找某个元素) * $k为要查找的关键字(注:待查找的数组元素为奇数个)$low为查找范围的最小键值,$high为查找范围的最大键值 ...

  6. leetcode@ [263/264] Ugly Numbers & Ugly Number II

    https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...

  7. poj2152 Fire

    好难啊,我弱爆了. 题解看陈启峰的论文... /** * Problem:POJ2152 * Author:Shun Yao * Time:2013.9.2 * Result:Accepted * M ...

  8. rsync 的安装

    Server setup 0)yum -y install xinetd vi /etc/xinetd.d/rsync and ensure following:        disable = n ...

  9. A Tour of Go Range continued

    You can skip the index or value by assigning to _. If you only want the index, drop the ", valu ...

  10. 【WIN32进阶之路】:内存映射文件

    第一章:源起  遇到一个问题,如果一个客户数据文件有2g大,客户要通过界面查询文件中的数据并用列表控件显示数据,要怎么处理这个文件才能让应用程序不会长时间无响应,客户感觉不到程序的卡顿? 第二章:解决 ...