您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页iOS App横竖屏混合完美解决方案(简单)

iOS App横竖屏混合完美解决方案(简单)

来源:二三四教育网

1、项目只设置支持横屏:

image.png

2、AppDelegate.h定义:

@property(nonatomic,assign)BOOL isRotationRight;//是否横屏

+ (AppDelegate *)appDelegate;

//设置横竖屏
- (void)setLandscapeRight:(BOOL)isRight;

3、AppDelegate.m实现

{
    self.isRotationRight = isRight;//(以上2行代码,可以理解为打开横屏开关)
    if (self.isRotationRight) {
        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"];
    }
}

#pragma mark - 横竖屏管理
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
    if (self.isRotationRight == YES) {
        return UIInterfaceOrientationMaskLandscapeRight;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}

4、哪个需要横屏调用:

[[AppDelegate appDelegate] setLandscapeRight:YES];

5、不需要横屏调用:

[[AppDelegate appDelegate] setLandscapeRight:NO];

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务