美文网首页UI 界面
iOS App横竖屏混合完美解决方案(简单)

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

作者: 刘明洋 | 来源:发表于2019-07-26 15:59 被阅读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 地址

    相关文章

      网友评论

        本文标题:iOS App横竖屏混合完美解决方案(简单)

        本文链接:https://www.haomeiwen.com/subject/xcrdrctx.html