如何同时支持IOS5和IOS6上的屏幕旋转?


http://blog.csdn.net/world_liu/article/details/8952522

转自:http://iliao.diandian.com/post/2013-02-21/40049597269



1.将addSubview修改为setRootViewController


[window addSubview: viewController.view];修改如下:


  1. if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)  
  2. {  
  3.    // warning: addSubView doesn't work on iOS6  
  4.    [window addSubview: viewController.view];  
  5. }  
  6. else  
  7. {  
  8.    // use this mehod on ios6  
  9.    [window setRootViewController:viewController];  
  10. }  


2.修改shouldAutorotateToInterfaceOrientation

 

  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.    return UIInterfaceOrientationIsLandscape( interfaceOrientation );  
  4. }  


iOS6以前会呼叫这方法来判断手机是垂直或打横

  1. // For ios6, use supportedInterfaceOrientations & shouldAutorotate instead of shouldAutorotateToInterfaceOrientation  
  2. - (NSUInteger) supportedInterfaceOrientations{  
  3.    return UIInterfaceOrientationMaskLandscape;  
  4. }  
  5. - (BOOL) shouldAutorotate {  
  6.    return YES;  
  7. }  

iOS6需在加上上面方法


在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,也就是说各个ViewController各自独立管理自己的旋转方向,跟其他ViewController或NavigationController没有关系,比如:

  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {//interfaceOrientation表示当前设备旋转到的方向,返回YES则表示界面也旋转到这个方向,NO则不  
  3.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  4. }  


但是在iOS6中,这个方法被废弃了,使用无效


在IOS6中的每个ViewController中代替使用:
  1. -(BOOL)shouldAutorotate  
  2. {  
  3.     return NO;//YES  支持旋转到supportedInterfaceOrientations函数所指定的方向  NO 则完全不支持旋转  始终保持正屏(UIInterfaceOrientationMaskPortrait)  
  4. }  
  5.   
  6.   
  7. -(NSUInteger)supportedInterfaceOrientations  
  8. {  
  9.     return UIInterfaceOrientationMaskPortrait; //当shouldAutorotate函数返回YES 指定的值才起作用  
  10. }  



这里有一个问题,当是用UINavigationController管理ViewController时,在各个viewController中制定的这两个函数必须要一样,不然会崩,于是一般自定义UINavigationController,然后在这里面重写这两个方法,这样之后,在每个具体ViewController中的指定就不会起作用,这样便于统一各个界面的旋转风格,但不适合有界面旋转风格不同的情况


for ios 4 and 5, 如果没有重写shouldAutorotateToInterfaceOrientation,那么对于iphone来讲,by default是只支持portrait,不能旋转。
for ios 6, 如果没有重写shouldAutorotate and supportedInterfaceOrientations,by default, iphone则是"可以旋转,支持非upside down的方向",而ipad是"可以选择,支持所有方向"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值