背景:
公司项目里要接入全景播放器,但是项目里所有的界面都只支持竖屏,但是播放器技术总监要求我必须能够设置播放器横屏播放。所以我在网络上找了很多前人的解决方法,但大都不能满足需求,然而也有可以用的方法可以使用,特粘贴出来供大家使用。
一、在视图管理器中设置横竖屏
此方法只适用于window的rootViewController为ViewController
-(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; }
二、在视图管理器中设置特定视图的横竖屏
此方法只适用于window的rootViewController为NavgationController或TabbarController
tab中:
-(BOOL)shouldAutorotate{ return [[self.viewControllers objectAtIndex:(int)self.selectedIndex] shouldAutorotate]; } -(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [[self.viewControllers objectAtIndex:(int)self.selectedIndex] supportedInterfaceOrientations]; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [[self.viewControllers objectAtIndex:(int)self.selectedIndex] preferredInterfaceOrientationForPresentation]; }
nav中:
-(BOOL)shouldAutorotate{ return [self.viewControllers.lastObject shouldAutorotate]; } -(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [self.viewControllers.lastObject supportedInterfaceOrientations]; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation]; }
然后在特定的viewController中实现方法一
三、建立单例对象管理全局的横竖屏
只需要设置Appdelegate控制视图横竖屏的代理方法具体实现方法如下:
先在app delegate里加上下面这个方法
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ return [[VRScreenTool sharedControl] mask]; }
然后创建个单例类VRScreenTool全局进行调用
@interface VRScreenTool : NSObject @property (nonatomic, assign) UIInterfaceOrientationMask mask; +(instancetype)sharedControl; -(void)setOrientation:(UIInterfaceOrientationMask)mask; @end
@implementation VRScreenTool +(instancetype)sharedControl{ static VRScreenTool *instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; }); return instance; } -(UIInterfaceOrientationMask)mask{ if (!_mask) { _mask = UIInterfaceOrientationMaskPortrait; } return _mask; } -(void)setOrientation:(UIInterfaceOrientationMask)mask { _mask = mask; } @end
然后在需要更改横竖屏的界面进行引用修改就可以了
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[VRScreenTool sharedControl] setOrientation:UIInterfaceOrientationMaskAllButUpsideDown]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[VRScreenTool sharedControl] setOrientation:UIInterfaceOrientationMaskPortrait]; }
当然你也可以直接调用appdelegte直接使用代理方法,但是个人不建议这样使用。