ios 横屏

来源:互联网 发布:k3帐套管理数据库设置 编辑:程序博客网 时间:2024/05/19 00:44

iOS横竖屏切换是一个很纠结的问题,之前项目中用到了,花了长时间查阅资料以及研究,才找到了一个相对靠谱的解决方案,该方案可以处理IOS9系统以上的屏幕翻转,至于IOS9一下的系统,还没有测试过。

为了过程的讲解,我先给出一个应用的需求:整个界面就显示一个按钮,当点击这个按钮的时候,界面能从竖屏切换到横屏,当再次点击的时候,又能从横屏切换到竖屏,之后点击循环往复。为了达到这样的需求,我们首先新建一个IOS工程。

在工程创建之后,需要设置应用支持的屏幕旋转方向。默认情况下,IOS支持四个方向的旋转,即Portrait、UpsideDown、LandscapeLeft、LandscapeRight。

可以在工程配置的General下面可以看到,也可以在info.plist里面进行配置



info.plist



配置好应用支持的屏幕旋转类型之后,接下来就需要在代码中配置横竖屏的情况。

这里很关键的一点是一定要配置rootViewController的横竖屏情况,特别是当rootViewController是NavigationViewController的时候,所以一定要对作为rootViewController的controller进行重写。下面给出的代码是对NavigationViewController的重写。

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  RotateNavigationController.h  
  3. //  RotateScreen  
  4. //  
  5. //  Created by obo on 16/1/28.  
  6. //  Copyright © 2016年 obo. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface RotateNavigationController : UINavigationController  
  12.   
  13. @property (nonatomic)UIInterfaceOrientation interfaceOrientation;  
  14. @property (nonatomic)UIInterfaceOrientationMask interfaceOrientationMask;  
  15.   
  16. @end  

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  RotateNavigationController.m  
  3. //  RotateScreen  
  4. //  
  5. //  Created by obo on 16/1/28.  
  6. //  Copyright © 2016年 obo. All rights reserved.  
  7. //  
  8.   
  9. #import "RotateNavigationController.h"  
  10.   
  11. @implementation RotateNavigationController  
  12.   
  13.   
  14. - (instancetype)initWithRootViewController:(UIViewController *)rootViewController {  
  15.       
  16.     self = [super initWithRootViewController:rootViewController];  
  17.       
  18.     if (self) {  
  19.         self.interfaceOrientation = UIInterfaceOrientationPortrait;  
  20.         self.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;  
  21.     }  
  22.       
  23.     return self;  
  24. }  
  25.   
  26. //设置是否允许自动旋转  
  27. - (BOOL)shouldAutorotate {  
  28.     return YES;  
  29. }  
  30.   
  31. //设置支持的屏幕旋转方向  
  32. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {  
  33.     return self.interfaceOrientationMask ;  
  34. }  
  35.   
  36. //设置presentation方式展示的屏幕方向  
  37. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  38.     return self.interfaceOrientation ;  
  39. }  
  40.   
  41.   
  42. @end  

重写实现NavigationViewController的shouldAutorate和supportedInterfaceOrientations这两个关键的方法,网上大部分建议都是让shouldAutorate返回NO,但实际上,返回NO的话,则会导致同一个界面的不能进行翻转,所以这里还是需要返回YES的。需要注意的是,interfaceOrientationMask和interfaceOrientation的初始值需要在controller初始化的时候进行赋值的,取值按照应用刚启动的时候屏幕翻转方向来设定。

之后,将这个NavigationViewController作为应用启动的rootViewController,在AppDelegate中的调用如下:

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  AppDelegate.m  
  3. //  RotateScreen  
  4. //  
  5. //  Created by obo on 16/1/28.  
  6. //  Copyright © 2016年 obo. All rights reserved.  
  7. //  
  8.   
  9. #import "AppDelegate.h"  
  10. #import "RotateNavigationController.h"  
  11. #import "ViewController.h"  
  12.   
  13. @interface AppDelegate ()  
  14.   
  15. @end  
  16.   
  17. @implementation AppDelegate  
  18.   
  19.   
  20. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  21.     // Override point for customization after application launch.  
  22.       
  23.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  24.     self.window.backgroundColor = [UIColor whiteColor];  
  25.   
  26.     RotateNavigationController *rotateNavigationController = [[RotateNavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];  
  27.     [rotateNavigationController setNavigationBarHidden:YES];  
  28.     self.window.rootViewController = rotateNavigationController;  
  29.       
  30.     return YES;  
  31. }  
  32.   
  33.   
  34. @end  

其中,ViewController是要显示的第一个界面,其内容的代码如下:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  ViewController.m  
  3. //  RotateScreen  
  4. //  
  5. //  Created by obo on 16/1/28.  
  6. //  Copyright © 2016年 obo. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10. #import "RotateNavigationController.h"  
  11.   
  12. @implementation ViewController  
  13.   
  14. - (void)viewDidLoad {  
  15.     [super viewDidLoad];  
  16.     [self initViews];  
  17. }  
  18.   
  19. - (void)initViews {  
  20.       
  21.     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10015010080)];  
  22.     [button setTitle:@"切换横竖屏" forState:UIControlStateNormal];  
  23.     button.backgroundColor = [UIColor redColor];  
  24.     [button addTarget:self action:@selector(clickToRotate) forControlEvents:UIControlEventTouchUpInside];  
  25.     [self.view addSubview:button];  
  26. }  
  27.   
  28. - (void)clickToRotate {  
  29.       
  30.     RotateNavigationController *navigationController = (RotateNavigationController *)self.navigationController;  
  31.     //切换rootViewController的旋转方向  
  32.     if (navigationController.interfaceOrientation == UIInterfaceOrientationPortrait) {  
  33.         navigationController.interfaceOrientation = UIInterfaceOrientationLandscapeRight;  
  34.         navigationController.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;  
  35.         //设置屏幕的转向为横屏  
  36.         [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];  
  37.     }  
  38.     else {  
  39.         navigationController.interfaceOrientation = UIInterfaceOrientationPortrait;  
  40.         navigationController.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;  
  41.         //设置屏幕的转向为竖屏  
  42.         [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];  
  43.     }  
  44.     //刷新  
  45.     [UIViewController attemptRotationToDeviceOrientation];  
  46. }  
  47.   
  48. - (BOOL)shouldAutorotate {  
  49.     return YES;  
  50. }  
  51.   
  52. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {  
  53.     return UIInterfaceOrientationMaskPortrait ;  
  54. }  
  55.   
  56. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  57.     return UIInterfaceOrientationPortrait;  
  58. }  
  59.   
  60. @end  

这个界面很简单,就一个按钮,每当点击的时候,设置一下RotateNavigationController的两个参数的值,并且切换一下屏幕的方向,同时,再刷新一下。这里的关键的方法是:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];  
这个方法能改变设备实际的旋转方向。

完成以上的几块内容,实际上就可以控制屏幕的横竖屏切换了。

本工程的例子已上传至github:IOS-Screenrotation-Control

0 0
原创粉丝点击