需求:项目要求是只支持竖屏的,但是视屏播放需要支持左右横屏。
首先,工程文件 -> General -> Device Orientation
选择 Portrait(正常竖屏)、Landscape Left (左横屏)、Landscape Right(右横屏);
如果在创建项目时,选择了 Devices:Universal,那么打开 info.plist 文件应该可以看到:
这里是配置横竖屏信息的地方,如果有不合适的地方可以进行调整。
然后,找到项目的根控制器,设置不支持屏幕旋转,并设置默认方向为正常反向。设置后会发现,凡是添加到根控制器的视图控制器都不再支持左右横屏。
方案一:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)
}
/**重点:
根控制器的位置:
1.如果在UIViewController上,则在UIViewController对应的.m文件中加入三个函数即可。
2.如果在UITabBarController上,则在UITabBarController对应的.m文件中加入三个函数即可。
3.如果在UINavigationController上,则在UINavigationController对应的.m文件中加入三个函数即可。
(不按情况执行,问题无法解决)
*/
其中,第一个方法可以省略,因为在iOS 6.0后这个方法就被废弃了:
// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED;
3、然后,实现MPMoviePlayerViewController的子类,并在 .m 文件中实现以下方法,就能实现需求了。
- (BOOL)shouldAutorotate
{
return Yes;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
方案二:
以导航控制器为跟控制器为例:
#pragma 横竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
- (BOOL)shouldAutorotate
{
if ([