標籤:
公司App裡面有個需求,即所有介面都是豎屏,且不允許橫屏切換,唯專屬一個播放視頻的介面允許橫屏,大家都知道視頻播放適配最大的播放螢幕那樣是最好的。從網上多方尋找資料,查到了這麼一篇文章:
最終,根據此需求處理如下: 首先,確保App本身應該允許轉屏切換:
我的App裡面UITabBarController是根視圖控制器,所以首先建立一個UITabBarController的子類,並設定允許轉屏:
(這些要放在根視圖控制器裡面, 如果你的應用根視圖是UINavigationController, 就放在那裡面就好)
#pragma mark 轉屏方法重寫-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [self.viewControllers.firstObject supportedInterfaceOrientations];}-(BOOL)shouldAutorotate{ return self.viewControllers.firstObject.shouldAutorotate;}
- 接著,我的tabbarController裡面每個子控制器又都是UINavigationController進行介面push切換的,所以首先建立一個UINavigationController的子類,並設定允許轉屏:
//self.topViewController是當前置航顯示的UIViewController,這樣就可以控制每個UIViewController所支援的方向啦!-(BOOL)shouldAutorotate{ return [self.topViewController shouldAutorotate];}- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations];}
- 在你想轉屏切換的ViewController上可以照這樣重寫(允許左右橫屏以及豎屏):
- (BOOL)shouldAutorotate { return NO;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait;}
- 到視頻播放介面要設定為橫屏啦(我的視訊播放只要橫屏就好^_^)
// 是否支援旋轉螢幕- (BOOL)shouldAutorotate { return NO;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape;}-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeRight;}
注意:跳轉到播放器要使用模態進來, 用Push會報錯
[self presentViewController:playerVC animated:YES completion:nil];
iOS9橫屏豎屏設定