前要
最近遇到很坑的问题,我们都遇到屏幕的横竖屏的情况,大部分的播放器的播放视频的时候都要支持横竖屏的操作,我由于用到了模态方式presentViewController
弹出其他控制器,进行其他的操作,完成之后再使用dismissViewControllerAnimated
收起模态出来的控制器,我的原来的控制器是支持屏幕自动旋转,支持横竖屏
//屏幕进行物理旋转的时候都会执行是否支持自动旋转
// 是否支持自动转屏
- (BOOL)shouldAutorotate{
return YES;
}
// 支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft |UIInterfaceOrientationPortrait;
}
但是模态弹出来的控制是只支持竖屏方向
-(BOOL)shouldAutorotate
{ //允许旋转
return NO;
}
// 支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
当我执行完所有操作的时候,然后dismissViewControllerAnimated
的这个时候程序就crash了,报错原因must match a supported interface orientation: 'landscapeLeft, landscapeRight'!
。
原因以及解决方案
从crash的原因描述,我们很清楚的看出来是屏幕的方向支持的不一样导致的,所以,使用模态弹出时候首先要保证弹出的控制器跟被弹出控制器的横竖屏的支持方向一致。@property (assign, nonatomic) BOOL isSupport_horizontal_screen;
这个属性默认的是YES(支持横竖屏),当我们presentViewController之前,设置isSupport_horizontal_screen为NO(只支持竖屏),dismissViewControllerAnimated
时候会调用-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
,只这个方法中再设置isSupport_horizontal_screen为YES,然后crash的就解决了。