iOS 横屏开发

2023-04-06  本文已影响0人  肉肉要次肉

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

注意:

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

}

上一篇 下一篇

猜你喜欢

热点阅读