整个项目是竖屏的,不能横屏,但是有个播放界面必须要横屏于是就开始找各种横屏的方法,最后在手机上好使了,但是在pad上横屏启动的时候界面是横屏显示,很是苦恼,就又开始了漫长的找资料,直接上代码
1,配置plist文件和deployemnt Info -> device orientation
(1),plist文件,如图所示,第一项是建立项目时默认有的表示支持手机的屏幕方向(我把支持向右和向左的删了),第二项是后加的表示支持ipad(添加 Supported interface orientations (iPad))的旋转方向(同样我把向左向右,向上的给删了)

(2),deployemnt Info -> device orientation,只选择第一项竖屏。

(1),AppDelegate.h里面
@property (assign , nonatomic) BOOL isForceLandscape;
@property (assign , nonatomic) BOOL isForcePortrait;
AppDelegate.m里面
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (self.isForceLandscape) {
return UIInterfaceOrientationMaskLandscape;
}else if (self.isForcePortrait){
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskPortrait;
}
(2),在相应的tabBarController里面(我是在viewController里面写的)
#import <UIKit/UIKit.h>
#import "BaseViewController.h"
@interface RootViewController : BaseViewController
@property (strong,nonatomic) UITabBarController *tabBarCon;
@end
-(BOOL)shouldAutorotate{
return [self.tabBarCon.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.tabBarCon.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return [self.tabBarCon.selectedViewController preferredInterfaceOrientationForPresentation];
}
(3),在navigationController里面(自己创建的navigationController的子类)
#import <UIKit/UIKit.h>
@interface RootNavigationController : UINavigationController
@property (nonatomic , assign) UIInterfaceOrientation interfaceOrientation;
@property (nonatomic , assign) UIInterfaceOrientationMask interfaceOrientationMask;
-(BOOL)shouldAutorotate{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.interfaceOrientationMask;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return self.interfaceOrientation;
}
(4)在父类viewController里面
- (void)viewDidLoad {
[super viewDidLoad];
[UIViewController attemptRotationToDeviceOrientation];
}
(5),在需要强制横屏的地方调用
- (void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden = YES;
self.navigationController.tabBarController.tabBar.hidden = YES;
[self forceOrientationLandscape];
RootNavigationController *nav = (RootNavigationController *)self.navigationController;
nav.interfaceOrientation = UIInterfaceOrientationLandscapeRight;
nav.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}
- (void)viewWillDisappear:(BOOL)animated{
[self forceOrientationPortrait];
RootNavigationController *navi = (RootNavigationController *)self.navigationController;
navi.interfaceOrientation = UIInterfaceOrientationPortrait;
navi.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}
#pragma mark 横屏设置
- (void)forceOrientationLandscape{
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForceLandscape=YES;
appdelegate.isForcePortrait=NO;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}
- (void)forceOrientationPortrait{
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForcePortrait=YES;
appdelegate.isForceLandscape=NO;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}