应用整体只支持竖屏,只有特定的某个界面支持横屏
解决方法:
1.在项目中plist文件中设置支持转屏方向
转屏控制级别: tabar>导航控制器>普通控制器
2.在tabbar/ 导航控制器/ 普通控制器 的.m文件中 复写以下三个方法
- (BOOL)shouldAutorotate ; // 是否支持屏幕自动旋转
-(UIInterfaceOrientationMask)supportedInterfaceOrientations // 支持的转屏方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // 进入后默认的屏幕方向(必须包含在支持的屏幕方向里)
1)TabBarVC 中重写三个方法的代码如下:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.selectedViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}
- (BOOL)shouldAutorotate {
return self.selectedViewController.shouldAutorotate;
}
2)导航控制器基类中重写三个方法的代码如下:
- (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;
}
针对没有tabbar,只有导航控制器的应用,可以直接省去TabBarVC中方法重写代码;
3.因为项目的大多控制器是不支持自动转屏,且只支持竖屏;因此这些ViewController 继承自BaseViewController;
针对特定的需要支持 左右横屏的视频播放界面,仍需要复写以上三个方法
@property(nonatomic,assign)BOOL autoRotate;
- (BOOL)shouldAutorotate{
return self.autoRotate;}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;}
旋转屏幕
- (void)switchToLandscapePotrait {
_autoRotate = YES;
// 如果当前设备是物理横屏,先恢复为竖屏,保证后面有转屏动画
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
}
// 手动设置横屏,会调用 方法 shouldAutorotate
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
[UIApplication sharedApplication].statusBarHidden = YES;
_autoRotate = NO;
}
代码手动设置横屏, [ [UIDevice currentDevice] setValue: forKey: ]会调用 方法- (BOOL)shouldAutorotate,如果该方法返回的是NO,则无法使用代码设置横屏;
所有需要在调用前 设置_autoRotate = YES; 调用完毕设置_autoRotate = YES;
注意事项:
当手机横放,已经是物理横屏的时候,再手动设置横屏是无效的;所以此处需要做处理(如果是物理横屏,先恢复为物理竖屏)