UI 界面

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

2019-07-26  本文已影响0人  刘明洋

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

Demo 地址

上一篇下一篇

猜你喜欢

热点阅读