现在的位置: 首页 > 综合 > 正文

问题解决:iOS6下shouldAutorotateToInterfaceOrientation不起作用,屏幕旋转同时支持iOS5和iOS6

2017年10月21日 ⁄ 综合 ⁄ 共 3998字 ⁄ 字号 评论关闭

在iOS6下shouldAutorotateToInterfaceOrientation被弃用,现在iOS6下有三个新方法处理屏幕旋转:

[plain] view
plain
copy

  1. // 是否支持屏幕旋转  
  2. - (BOOL)shouldAutorotate {  
  3.     return YES;  
  4. }  
  5. // 支持的旋转方向  
  6. - (NSUInteger)supportedInterfaceOrientations {  
  7.     return UIInterfaceOrientationMaskAllButUpsideDown;//UIInterfaceOrientationMaskAllButUpsideDown;  
  8. }  
  9. // 一开始的屏幕旋转方向  
  10. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  11.     return UIInterfaceOrientationPortrait;  
  12. }  


现在屏幕旋转支持iOS5和iOS6需要写两份代码,具体步骤如下:

一、在info.plist的“Supported interface orientations”添加支持的旋转方向,这个必须有,决定了旋转支持的最多方向;

二、将AppDelegate的[self.window addSubview:navigationController.view]; 改为self.window.rootViewController = navigationController; 或者[self.window addSubview:tabBarController.view]; 改为self.window.tabBarController;

三、在AppDelegate下面添加UINavigationController(或者UITabBarController) 和 UIViewController的类别Rotation_IOS6:

[plain] view
plain
copy

  1. // UIViewController  
  2. @implementation UIViewController (Rotation_IOS6)  
  3.   
  4. // IOS5默认支持竖屏  
  5. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
  6.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  7. }  
  8.   
  9. // IOS6默认不开启旋转,如果subclass需要支持屏幕旋转,重写这个方法return YES即可  
  10. - (BOOL)shouldAutorotate {  
  11.     return NO;  
  12. }  
  13.   
  14. // IOS6默认支持竖屏  
  15. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  16.     return UIInterfaceOrientationPortrait;  
  17. }  
  18.   
  19. @end  
  20.   
  21. // UINavigationController  
  22. @implementation UINavigationController (Rotation_IOS6)  
  23.   
  24. - (BOOL)shouldAutorotate {  
  25.     return [[self.viewControllers lastObject] shouldAutorotate];  
  26. }  
  27.   
  28. - (NSUInteger)supportedInterfaceOrientations {  
  29.     return [[self.viewControllers lastObject] supportedInterfaceOrientations];  
  30. }  
  31.   
  32. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  33.     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];  
  34. }  
  35.   
  36. @end  
  37.   
  38. // UITabBarController  
  39. @implementation UITabBarController (Rotation_IOS6)  
  40.   
  41. - (BOOL)shouldAutorotate {  
  42.     return [[self.viewControllers lastObject] shouldAutorotate];  
  43. }  
  44.   
  45. - (NSUInteger)supportedInterfaceOrientations {  
  46.     return [[self.viewControllers lastObject] supportedInterfaceOrientations];  
  47. }  
  48.   
  49. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  50.     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];  
  51. }  
  52.   
  53. @end  



四、在需要支持屏幕旋转的地方添加:

[plain] view
plain
copy

  1. #pragma mark - IOS 5 Rotation  
  2. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
  3.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  4. }  
  5. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {  
  6.     [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];  
  7.   
  8. }  
  9. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {  
  10.     [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];  
  11. }  
  12. #pragma mark - IOS 6 Rotation  
  13. - (BOOL)shouldAutorotate {  
  14.     return YES;  
  15. }  
  16.   
  17. - (NSUInteger)supportedInterfaceOrientations {  
  18.     return UIInterfaceOrientationMaskAllButUpsideDown;//UIInterfaceOrientationMaskAllButUpsideDown;  
  19. }  
  20.   
  21. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  22.     return UIInterfaceOrientationPortrait;  
  23. }  

参考:http://blog.hanpo.tw/2012/09/ios-60-orientation.htmlhttp://www.cocoachina.com/bbs/read.php?tid=116091&keyword=ios6

具体见Demo:http://vdisk.weibo.com/s/k-5g1


修改于:2012-12-17,IOS6下MPMoviePlayerViewController支持旋转,需要在AppDelegate里面添加类别Rotation_IOS6:

[plain] view
plain
copy

  1. // MPMoviePlayerViewController  
  2. @implementation MPMoviePlayerViewController (Rotation_IOS6)  
  3.   
  4. - (BOOL)shouldAutorotate {  
  5.     return YES;  
  6. }  
  7.   
  8. // IOS6默认支持竖屏  
  9. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
  10.     return UIInterfaceOrientationPortrait;  
  11. }  
  12.   
  13. @end  


原文链接:http://www.dapps.net/dev/iphone/ios-6-supporting-multiple-interface-orientations.html

抱歉!评论已关闭.