支持横竖屏的使用dismissViewControllerAnimated的时候crash的解决方案

本文介绍了一种解决iOS应用中模态视图控制器与原控制器横竖屏方向不一致导致的崩溃问题的方法。通过设置一个布尔属性控制横竖屏支持情况,在显示和隐藏模态视图时调整此属性,确保控制器间屏幕方向一致。

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

前要

最近遇到很坑的问题,我们都遇到屏幕的横竖屏的情况,大部分的播放器的播放视频的时候都要支持横竖屏的操作,我由于用到了模态方式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的就解决了。


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值