iOS 横竖屏转换 强制横屏

之前写过进入从A页面进入B页面,B页面直接进入就是横屏的情况,使用一个方法就可以了,但是昨天又遇到这个需求的时候又用这个方法就不好使了。当时用的时大概是2年前,现在不知为何不好使了。

#pragma mark -开始就变成横屏

//返回直接支持的方向

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

returnUIInterfaceOrientationMaskLandscapeRight;

}

//返回最优先显示的屏幕方向

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

return(toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight);

}

//Interface的方向是否会跟随设备方向自动旋转,如果返回NO,后两个方法不会再调用

- (BOOL)shouldAutorotate

{

returnYES;

}

后来百度从哪个大神那里知道是因为只有在一下两种情况下使用才好使

1.当前viewController是window的rootViewController。

2.当前viewController是modal模式的,即此viewController是调用presentModalViewController而显示出来的.

但是程序往往并不能满足上诉的两种情况,最后找到了下面的方法,这个方法写在刚进入B页面的时候,再加上上面的3个方法就可以成功的把屏幕转成横屏了。

if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)]) {

SELselector =NSSelectorFromString(@"setOrientation:");

NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];

[invocationsetSelector:selector];

[invocationsetTarget:[UIDevicecurrentDevice]];

intval =UIInterfaceOrientationLandscapeRight;

[invocationsetArgument:&valatIndex:2];

[invocationinvoke];

}

在离开页面的时候,需要将页面再转回来

if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)]) {

SELselector =NSSelectorFromString(@"setOrientation:");

NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];

[invocationsetSelector:selector];

[invocationsetTarget:[UIDevicecurrentDevice]];

intval =UIDeviceOrientationPortrait;

[invocationsetArgument:&valatIndex:2];

[invocationinvoke];

}

完成👌

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容