iOS 6 中转屏API的改变

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

把上面的代码放入工程中,经笔者测试,有效~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值