ios屏幕旋转

本文介绍如何在iOS应用中实现特定界面支持横屏旋转,其他界面保持竖屏。通过设置AppDelegate属性和控制界面转屏代码,实现灵活的屏幕方向管理。在iPad上可能遇到的问题及解决方案也一并提及。
摘要由CSDN通过智能技术生成

目标:app只在个别界面支持屏幕的横向旋转,别的界面都是只能竖屏

1.首先勾选手机支持的旋转方向

2. AppDelegate.h中设置一个属性 

@property(nonatomic,assign)BOOL allowRotation;//是否允许在横向上旋转

AppDelegate.m中实现如下方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window{

    if (_allowRotation == YES) {  // 如果属性值为YES,则允许屏幕旋转

        return UIInterfaceOrientationMaskLandscape;  // 这里是屏幕要旋转的方向(只能在横屏方向旋转)

    }else{

        return (UIInterfaceOrientationMaskPortrait);//只能竖屏

    }

}

 3.在需要横向旋转的界面做一下设置

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

    appDelegate.allowRotation = YES;//(以上2行代码,可以理解为打开横屏开关)

    [self setNewOrientation:YES];//调用转屏代码(即一旦进入该界面就成为横屏)

#pragma mark--- 设置屏幕旋转

- (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"];

    }

}

4.在退出横屏界面是作如下设置

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"];

    }

}

 

在IPad不能横屏问题处理,勾选上如下选项

ios开发技术,提交审核讨论群 加qq群:695614778 一起讨论 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

义中

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值