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 currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}else{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==UIInterfaceOrientationLandscapeRight);
}
}
#endif
我到觉得这算是一个ios不合理的方法。
如果大家有好的方式或者解释文档,请在评论里给出答案,以解答我的疑惑。。。感谢大家