貌似算是一个iOS的bug,很头疼

本文介绍了如何在iOS应用中实现对于iOS5和iOS6不同版本屏幕旋转的支持。通过使用条件编译和正确的方法实现,确保了应用在iPhone和iPad上的稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS 6 版本之后屏幕旋转方法发生了变化,一不小心陷入了一个坑里,头疼了半天没找到原因,找到原因之后一顿唉声叹气。。。。貌似算是iOS一个不合理的bug.

要兼容iOS5和iOS6屏幕旋转应该这样写:

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1

- (BOOL)shouldAutorotate

{

    return YES;

}

-(NSUInteger)supportedInterfaceOrientations

{

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        return UIInterfaceOrientationMaskAllButUpsideDown;

    }else{

        return UIInterfaceOrientationMaskLandscape;

    }

}

#endif

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;

    }else{

        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);

    }

}

这样写在ipad, iphone上ios5和ios6版本都是没有问题的。可关键的是习惯了if else写法,如果写成如下方式就痛苦了。。。

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1

- (BOOL)shouldAutorotate

{

    return YES;

}

-(NSUInteger)supportedInterfaceOrientations

{

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        return UIInterfaceOrientationMaskAllButUpsideDown;

    }else{

        return UIInterfaceOrientationMaskLandscape;

    }

}

#else //这个else在ios5版本上是跑不进来的

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    if ([[UIDevice currentDeviceuserInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;

    }else{

        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==UIInterfaceOrientationLandscapeRight);

    }

}

#endif

我到觉得这算是一个ios不合理的方法。

如果大家有好的方式或者解释文档,请在评论里给出答案,以解答我的疑惑。。。感谢大家



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值