iOS 横屏开发

本项目只是个别页面需要横屏,所以不需要勾选方向,默认竖屏即可。

注意:

1、iOS16以下版本:只支持present跳转页面,并且是要设置scanVC.modalPresentationStyle = UIModalPresentationFullScreen;才会进入页面自动横屏生效

2、iOS16版本:push,present都可以(亲测iOS16.4)

1.创建单例(RotationManager)用于管理界面横竖屏状态

.h代码

//单例类

+(instancetype)shareInstance;

//是否横屏

@property(nonatomic,assign)BOOL isRotation;

//当前屏幕状态

+(UIInterfaceOrientationMask)supportedInterfaceOrientationsType;

.m代码

static RotationManager*_manager;

//单例方法

+(instancetype)shareInstance{

    staticdispatch_once_t onceToken;

    dispatch_once(&onceToken,^{

       _manager=[[RotationManager alloc]init];

      _manager.isRotation=NO;

   });

return_manager;

}

//查询需要的屏幕状态

+(UIInterfaceOrientationMask)supportedInterfaceOrientationsType{

     if(_manager.isRotation){

          returnUIInterfaceOrientationMaskLandscape;

      }

      returnUIInterfaceOrientationMaskPortrait;

}

2.vc页调用

记得在哪页需要,就在哪页引入头文件 #import "RotationManager.h"

-(void)viewDidLoad{

 [superviewDidLoad];

//开启横屏状态

[RotationManager shareInstance].isRotation=YES;

}

-(void)viewWillDisappear:(BOOL)animated{

[superviewWillDisappear:animated];

//一定要记得关闭横屏状态,不然退出界面后依旧是横屏

[RotationManager shareInstance].isRotation=NO;

}

-(BOOL)shouldAutorotate{

returnYES;

}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{

   return   UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft;

}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{

   return   UIInterfaceOrientationLandscapeRight;

}

3.AppDelegate

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

   return [RotationManager supportedInterfaceOrientationsType];

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容