本項目只是個別頁面需要橫屏饲齐,所以不需要勾選方向缓窜,默認豎屏即可藤违。
注意:
1、iOS16以下版本:只支持present跳轉(zhuǎn)頁面脊岳,并且是要設(shè)置scanVC.modalPresentationStyle = UIModalPresentationFullScreen;才會進入頁面自動橫屏生效
2逝段、iOS16版本:push,present都可以(親測iOS16.4)
1.創(chuàng)建單例(RotationManager)用于管理界面橫豎屏狀態(tài)
.h代碼
//單例類
+(instancetype)shareInstance;
//是否橫屏
@property(nonatomic,assign)BOOL isRotation;
//當前屏幕狀態(tài)
+(UIInterfaceOrientationMask)supportedInterfaceOrientationsType;
.m代碼
static RotationManager*_manager;
//單例方法
+(instancetype)shareInstance{
? ? staticdispatch_once_t onceToken;
? ? dispatch_once(&onceToken,^{
? ? ? ?_manager=[[RotationManager alloc]init];
? ? ? _manager.isRotation=NO;
? ?});
return_manager;
}
//查詢需要的屏幕狀態(tài)
+(UIInterfaceOrientationMask)supportedInterfaceOrientationsType{
? ? ?if(_manager.isRotation){
? ? ? ? ? returnUIInterfaceOrientationMaskLandscape;
? ? ? }
? ? ? returnUIInterfaceOrientationMaskPortrait;
}
2.vc頁調(diào)用
記得在哪頁需要,就在哪頁引入頭文件 #import "RotationManager.h"
-(void)viewDidLoad{
?[superviewDidLoad];
//開啟橫屏狀態(tài)
[RotationManager shareInstance].isRotation=YES;
}
-(void)viewWillDisappear:(BOOL)animated{
[superviewWillDisappear:animated];
//一定要記得關(guān)閉橫屏狀態(tài)割捅,不然退出界面后依舊是橫屏
[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];
}