iOS界面設置豎屏,部分界面支持橫豎屏

背景:

公司項目里要接入全景播放器,但是項目里所有的界面都只支持豎屏,但是播放器技術總監要求我必須能夠設置播放器橫屏播放。所以我在網絡上找了很多前人的解決方法,但大都不能滿足需求,然而也有可以用的方法可以使用,特粘貼出來供大家使用。

一、在視圖管理器中設置橫豎屏

此方法只適用于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直接使用代理方法,但是個人不建議這樣使用。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1.監聽屏幕旋轉方向 在處理iOS橫豎屏時,經常會和UIDeviceOrientation、UIInterface...
    彬至睢陽閱讀 2,547評論 1 6
  • 最近有一個項目,例如:A界面跳轉到B界面,A界面是豎屏的,B界面進入就要橫屏。 花了半天的時間在網上搜索解決方案,...
    手中的風信子閱讀 20,792評論 6 29
  • iOS 中橫豎屏切換的功能,在開發iOS app中總能遇到。以前看過幾次,感覺簡單,但是沒有敲過代碼實現,最近又碰...
    零度_不結冰閱讀 2,215評論 0 0
  • 一、橫豎屏有控制優先級,一旦優先級高的關閉了橫豎屏配置,優先級低的無論如何配置都無法做到橫豎屏。 對于限于VC范圍...
    EdenMa閱讀 381評論 0 0
  • 數據壓縮 概況 本章描述了幾種數據壓縮選項,它能夠幫助你減少磁盤空間的使用,在某種情況下,還可以提高I/O性能。 ...
    LestatZ閱讀 1,270評論 0 1