在iOS6下shouldAutorotateToInterfaceOrientation被弃用,现在iOS6下有三个新方法处理屏幕旋转:
// 是否支持屏幕旋转 - (BOOL)shouldAutorotate { return YES; } // 支持的旋转方向 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown;//UIInterfaceOrientationMaskAllButUpsideDown; } // 一开始的屏幕旋转方向 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }
现在屏幕旋转支持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:
// UIViewController @implementation UIViewController (Rotation_IOS6) // IOS5默认支持竖屏 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } // IOS6默认不开启旋转,如果subclass需要支持屏幕旋转,重写这个方法return YES即可 - (BOOL)shouldAutorotate { return NO; } // IOS6默认支持竖屏 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @end // UINavigationController @implementation UINavigationController (Rotation_IOS6) - (BOOL)shouldAutorotate { return [[self.viewControllers lastObject] shouldAutorotate]; } - (NSUInteger)supportedInterfaceOrientations { return [[self.viewControllers lastObject] supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; } @end // UITabBarController @implementation UITabBarController (Rotation_IOS6) - (BOOL)shouldAutorotate { return [[self.viewControllers lastObject] shouldAutorotate]; } - (NSUInteger)supportedInterfaceOrientations { return [[self.viewControllers lastObject] supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; } @end
四、在需要支持屏幕旋转的地方添加:
#pragma mark - IOS 5 Rotation - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; } #pragma mark - IOS 6 Rotation - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown;//UIInterfaceOrientationMaskAllButUpsideDown; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }参考: http://blog.hanpo.tw/2012/09/ios-60-orientation.html和 http://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:
// MPMoviePlayerViewController @implementation MPMoviePlayerViewController (Rotation_IOS6) - (BOOL)shouldAutorotate { return YES; } // IOS6默认支持竖屏 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @end
修改于:2013-1-24,IOS6下调用presentViewController:animated:completion:
显示UIImagePickerController奔溃问题,需要在AppDelegate里面添加类别Rotation_IOS6:
// UIImagePickerController @implementation UIImagePickerController (Rotation_IOS6) - (BOOL)shouldAutorotate { return NO; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @end
修改于:2013-3-29,完整代码:
#pragma mark - Rotation category // UIViewController @implementation UIViewController (Rotation_IOS6) // IOS5默认支持竖屏 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } // IOS6默认不开启旋转,如果subclass需要支持屏幕旋转,重写这个方法return YES即可 - (BOOL)shouldAutorotate { return NO; } // IOS6默认支持竖屏 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @end // UINavigationController @implementation UINavigationController (Rotation_IOS6) - (BOOL)shouldAutorotate { return [[self.viewControllers lastObject] shouldAutorotate]; } - (NSUInteger)supportedInterfaceOrientations { return [[self.viewControllers lastObject] supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; } @end // UITabBarController @implementation UITabBarController (Rotation_IOS6) - (BOOL)shouldAutorotate { return [[self.viewControllers lastObject] shouldAutorotate]; } - (NSUInteger)supportedInterfaceOrientations { return [[self.viewControllers lastObject] supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; } @end // MPMoviePlayerViewController @implementation MPMoviePlayerViewController (Rotation_IOS6) // IOS6默认开启旋转 - (BOOL)shouldAutorotate { return YES; } // IOS6默认支持竖屏 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @end // UIImagePickerController @implementation UIImagePickerController (Rotation_IOS6) // IOS6默认不开启旋转,如果subclass需要支持屏幕旋转,重写这个方法return YES即可 - (BOOL)shouldAutorotate { return NO; } // IOS6默认支持竖屏 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @end