强制横屏(仅适用于present情景)

39 篇文章 0 订阅

设置横屏很简单,只需要勾选上这两项就可以了

但这是设置全局的横屏允许,很多时候需求是大多界面只允许竖屏,只有某个界面才需要设置横屏(例如播放视频界面等),所以这时这两项就不能勾选上,那要怎么设置横屏呢,其实很简单,当然本文的方法只适用present视图

首先在appdelegate中添加一个属性

@property (nonatomic,assign) BOOL allowRotation;

然后添加代理方法

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskPortrait;
}

在需要设置横屏的视图present前设置为YES

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotation = YES;

在横屏视图dismiss前设置为NO即可恢复竖屏

随意转换横竖屏(present 视频播放时全屏转换)

//修改全局属性并调用转屏方法
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;     
appDelegate.allowRotation = NO;      
[self setNewOrientation:NO];//调用转屏代码

//强制转屏
- (void)setNewOrientation:(BOOL)fullscreen

{
    if (fullscreen) {

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];


        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];//横屏方向(根据需求选填)

        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }else{

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];


        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];

        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值