iOS6中横屏的处理方法

简介: iOS6中横屏的处理方法

IOS6以后,若想在项目中支持横屏,我们首先需要在plist文件中添加支持横屏的设置,否则有些代码设置将会失效。


有来那个方式设置:


1、在pilist的Supported interface orientations 字段中添加

image.png


2、在Xcode的设置中勾选

image.png


现在我们来看决定屏幕方向的几个函数:


在IOS6之前,我们只需通过一个函数


- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

   return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);

}


就可以支持指定控制器的旋转。通过新的文档,我们可以看到:


// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

    NS_DEPRECATED_IOS(2_0, 6_0);

//这个方法在6.0之后被标记为过时的

我们通过下面两个方法来代替:


//是否允许屏幕旋转


-(BOOL)shouldAutorotate{

   return YES;

}

//支持的方向

- (NSUInteger)supportedInterfaceOrientations {

   return UIInterfaceOrientationMaskLandscapeRight;

}

这是个枚举


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),

};

通过这两个函数,如果我们需要某个控制器强制方向,我们可以设置支持单一的方向,即可达到目的。


注意:


如果你们项目中的RootViewController是导航,你会发现,你在Push出来的视图中添加刚才的代码并没有起作用,原因是导航,并没有进行设置,我们创建一个文件,继承于NavigationController。在里面重写刚才的方法,这么做后,屏幕确实横了过来,并且这个导航push的所有子界面都将横屏,这也不是我们想要的效果。我们想自由的控制每个push出来的界面的屏幕方向,可以在导航里这么做:


-(BOOL)shouldAutorotate{

   return [self.topViewController shouldAutorotate];

}

//支持的方向

- (NSUInteger)supportedInterfaceOrientations {

   return [self.topViewController supportedInterfaceOrientations];;

}

我们还需要做一些处理,经过我的测试,导航必须在pop后才会重新调用这些函数,所以我的方法是这样做:弹出一个中间控制器后再POP回来


@implementation ViewController2


- (void)viewDidLoad {

   [super viewDidLoad];

   // Do any additional setup after loading the view.

   [self.navigationController pushViewController:[[ViewController3 alloc]init] animated:YES];

}

@implementation ViewController3


- (void)viewDidLoad {

   [super viewDidLoad];

   // Do any additional setup after loading the view.

   [self.navigationController popViewControllerAnimated:YES];

}

这样做,我们就可以自由的控制每个视图控制器的方向了。


同理,如果根视图控制器是tabBar,则我们需要在tabBar中做操作。


如果我们大多是的视图控制器都是一个方向的,只有偶尔的几个会不同,这时候,我们其实可以采取presentationController的方式,然后直接在弹出的控制器中写那两个方法即可。这是最简单的途径了。

目录
相关文章
在使用蓝牙接口,遇到IOS下正常,Android下不正常的简易处理方法
如果遇到以上的情况怎么办,先确定下在调试的时候是否打开了调试面板, 如果有打开请关闭调试面板看是否还有问题,目前在安卓上打开调试面板是会有影响到蓝牙接口的使用,从之前遇到过这些问题的统计中也确实是因为这个原因
516 0
|
iOS开发
iOS post提交数据有嵌套数组的处理方法
2017年11月21日17:11:43 解决办法, 修改iOS框架里的代码: http://www.jianshu.com/p/130daa0c2fe7 确实有效, 要不然,  内层的每一个键值对都会变成一个单独的dic, 这样原来的两层就变成了3层:     ===>
1201 0
|
iOS开发
iOS接收null的处理方法
经常服务器返回的数据,有null,还有nil,如果在模型层不处理的话,到时候数据展现时,一定会崩啊,最近决心要解决这个问题,所以查看了一些资料后,有答案了: - (id) setNoNull:(id)aValue{ if (aValue == ...
941 0
|
iOS开发 C++
ios 中弹窗的编程 和 一些处理方法
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> 1. 在ios开发中使用 弹窗来显示信息。</p> <p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"
1578 0
|
iOS开发 API
iOS 横屏竖屏开发相关
引言: iPhone的横屏竖屏针对iOS系统版本分为两种开发方式: 一种是iOS 6之前的使用模式 一种是iOS6的新模式. 两者的区别还是蛮大的. 参考: 1:IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统) http://blog.
1170 0
|
10月前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。