iOS5,iOS6,iOS7中UITabBarController,UINavigationController等界面中的旋转问题总结

来源:互联网 发布:淘宝美工和客服哪个好 编辑:程序博客网 时间:2024/05/18 13:45

今天需要在程序中的一个界面中实现横屏和竖屏切换,而其他界面保持竖屏,实现的过程中遇到了若干问题,总结了一下,在这里分享给大家。

遇到的问题如下:

1.如何在其中一个UIViewController中实现横竖屏切换,其他UIViewController仍然只支持竖屏。

2.无论如何设置参数,所有界面都不支持横竖屏切换。

3.界面中的横竖屏切换正常,但是启动页面始终是横屏。


在解决以上问题之前,先说明一下设置设备旋转的方式。

1.iOS5、iOS6、iOS7都可以在程序的info.plist中增加Supported interface orientations字段,在该字段的值中增加所需要的方向。

2.iOS6和iOS7可以在AppDelegate中增加以下代码,设置程序支持的方向:

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

以上两个方式是针对整个应用的,下面介绍的方法针对单个界面。

3.iOS5以前,可以通过在UIViewController或其子类(UITabBarController、UINavigationController等)中重写以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}

4.在iOS6之后,可以通过在UIViewController或其子类(UITabBarController、UINavigationController等)中重写以下方法:

-(NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskAllButUpsideDown;}-(BOOL)shouldAutorotate{    return YES;}

所以通过将以上全局和单个页面的方法结合,我们就可以实现只在某个页面中实现横竖屏切换。那下面我们就来说说如何解决一开始提出的三个问题:

1.当程序中存在UITabBarController时,可以通过category或者继承UITabBarController,覆盖以下方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    // You do not need this method if you are not supporting earlier iOS Versions    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];}-(NSUInteger)supportedInterfaceOrientations{    return [self.selectedViewController supportedInterfaceOrientations];}-(BOOL)shouldAutorotate{    return YES;}

当程序中存在UINavigationController时,可以通过category或者继承UINavigationController,覆盖以下方法:

-(NSUInteger)supportedInterfaceOrientations{    return [self.topViewController supportedInterfaceOrientations];}-(BOOL)shouldAutorotate{    return YES;}

最后,就可以在需要横竖屏切换的页面中实现以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}-(NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskAllButUpsideDown;}-(BOOL)shouldAutorotate{    return YES;}

在不需要横竖屏切换的页面中实现以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation == UIInterfaceOrientationPortrait);}-(BOOL)shouldAutorotate{    return NO;}-(NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskPortrait;}

如果觉得页面过多的话,可以给所有页面加一个基类,把不需要横竖屏切换的代码加到里面,再在需要横竖屏切换的页面中覆盖基类的方法即可。


2.第二个问题解决方法让我郁闷的时间最长,但是解决的时间也是最快的,原因是我的手机平时都默认竖屏锁定,解开就好了。。。


3.第三个问题在Info.plist中增加字段和AppDelegate中增加代码两种方式的区别。这两种方式的实现效果是一样的,都可以增加全局支持的方向。需要注意的是,AppDelegate中增加的代码仅在iOS6之后的系统生效,如果要支持iOS5以下的系统,还是需要在Info.plist中增加字段。如果两种方式都实现时,所增加的方向以AppDelegate中增加的代码为准;而启动屏幕的方向,以在Info.plist中字段顺序为准。

举个例子,如果在Info.plist中增加字段的顺序为Landscape(right), Landscape(left), Portrait,则启动的时候为横屏;如果在Info.plist中增加字段的顺序为Portrait, Landscape(right), Landscape(left),则启动的时候为竖屏。


如果大家觉得对自己有帮助的话,还希望能帮顶一下,谢谢:) 个人博客:http://blog.csdn.net/zhaoxy2850 本文地址:http://blog.csdn.net/zhaoxy_thu/article/details/18412661 转载请注明出处,谢谢! 
0 0