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

在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.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:

// 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

具体见: http://stackoverflow.com/questions/12522491/crash-on-presenting-uiimagepickercontroller-under-ios6

修改于: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




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值