iOS 6 中的转屏API发生了变化,以前的控制转屏的回调已经不再响应:
1 @interface UIViewController (UIViewControllerRotation) 2 3 // Applications should use supportedInterfaceOrientations and/or shouldAutorotate.. 4 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0); 5 6 // New Autorotation support. 7 - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0); 8 - (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0); 9 // Returns interface orientation masks. 10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);
以前常用的shouldAutorotateToInterfaceOrientation方法从6.0版本开始,已经被打上DEPRECATED标签,这意味着原本的应用运行在iOS6设备上是,上面的方法不再被系统调用了。取而代之的是后面标有NS_AVAILABLE_IOS(6_0)的几个方法,更准确的说,可以用supportedInterfaceOrientations方法代替。需要同时支持iOS 6 和之前版本的设备时,两个方法都需要实现。
在使用UINavigationController时发现,无论怎么设置上面方法的返回,都无法控制UINavigationController的旋转,这似乎是iOS的一个bug。但无论如何,以下是stackflow上面的一个解决方法:
1 @implementation UINavigationController (Rotation_IOS6) 2 -(BOOL)shouldAutorotate { 3 return [[self.viewControllers lastObject] shouldAutorotate]; 4 } 5 6 -(NSUInteger)supportedInterfaceOrientations { 7 return [[self.viewControllers lastObject] supportedInterfaceOrientations]; 8 } 9 10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 11 return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 12 } 13 @end
把上面的代码放入工程中,经笔者测试,有效~