應(yīng)用整體只支持豎屏涝滴,只有特定的某個(gè)界面支持橫屏
解決方法:
1.在項(xiàng)目中plist文件中設(shè)置支持轉(zhuǎn)屏方向
轉(zhuǎn)屏控制級(jí)別: tabar>導(dǎo)航控制器>普通控制器
2.在tabbar/ 導(dǎo)航控制器/ 普通控制器 的.m文件中 復(fù)寫(xiě)以下三個(gè)方法
- (BOOL)shouldAutorotate ; //? 是否支持屏幕自動(dòng)旋轉(zhuǎn)
-(UIInterfaceOrientationMask)supportedInterfaceOrientations // 支持的轉(zhuǎn)屏方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation? // 進(jìn)入后默認(rèn)的屏幕方向(必須包含在支持的屏幕方向里)
1)TabBarVC 中重寫(xiě)三個(gè)方法的代碼如下:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.selectedViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}
- (BOOL)shouldAutorotate {
return self.selectedViewController.shouldAutorotate;
}
2)導(dǎo)航控制器基類中重寫(xiě)三個(gè)方法的代碼如下:
- (BOOL)shouldAutorotate{
return self.topViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return self.topViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return self.topViewController.preferredInterfaceOrientationForPresentation;
}
3)普通控制器基類中代碼如下:
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
針對(duì)沒(méi)有tabbar恶迈,只有導(dǎo)航控制器的應(yīng)用臣咖,可以直接省去TabBarVC中方法重寫(xiě)代碼跃捣;
3.因?yàn)轫?xiàng)目的大多控制器是不支持自動(dòng)轉(zhuǎn)屏,且只支持豎屏夺蛇;因此這些ViewController 繼承自BaseViewController枝缔;
針對(duì)特定的需要支持 左右橫屏的視頻播放界面,仍需要復(fù)寫(xiě)以上三個(gè)方法
@property(nonatomic,assign)BOOL? ? ? ? ? ? ? ? ? ? autoRotate;
- (BOOL)shouldAutorotate{
return self.autoRotate;}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;}
旋轉(zhuǎn)屏幕
- (void)switchToLandscapePotrait {
_autoRotate = YES;
//? 如果當(dāng)前設(shè)備是物理橫屏,先恢復(fù)為豎屏,保證后面有轉(zhuǎn)屏動(dòng)畫(huà)
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
}
// 手動(dòng)設(shè)置橫屏,會(huì)調(diào)用 方法 shouldAutorotate
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
[UIApplication sharedApplication].statusBarHidden = YES;
?_autoRotate = NO;
}
代碼手動(dòng)設(shè)置橫屏, ? [ [UIDevice currentDevice] setValue: forKey: ]會(huì)調(diào)用? 方法- (BOOL)shouldAutorotate,如果該方法返回的是NO,則無(wú)法使用代碼設(shè)置橫屏;
所有需要在調(diào)用前 設(shè)置_autoRotate = YES;? 調(diào)用完畢設(shè)置_autoRotate = YES;?
注意事項(xiàng):?
當(dāng)手機(jī)橫放,已經(jīng)是物理橫屏的時(shí)候,再手動(dòng)設(shè)置橫屏是無(wú)效的;所以此處需要做處理(如果是物理橫屏,先恢復(fù)為物理豎屏)