背景:
公司項目里要接入全景播放器,但是項目里所有的界面都只支持豎屏噩峦,但是播放器技術總監(jiān)要求我必須能夠設置播放器橫屏播放锭沟。所以我在網絡上找了很多前人的解決方法,但大都不能滿足需求识补,然而也有可以用的方法可以使用族淮,特粘貼出來供大家使用。
一、在視圖管理器中設置橫豎屏
此方法只適用于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]; }
然后創(chuàng)建個單例類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直接使用代理方法,但是個人不建議這樣使用较幌。