因为公司项目要兼顾老版本的原因,有的需要横屏显示有的需要竖屏显示,这就带来很麻烦的事了,测试了几种方法,终于搞定,直接铺代码!
一、UINavigationController 设置
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight);
}
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
returnself.topViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return self.topViewController.preferredInterfaceOrientationForPresentation;
}
在需要横屏的VC里设置
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self forceOrientationLandscape];
}
/**
Description 强行横屏设置
*/
- (void)forceOrientationLandscape
{
AppDelegate *appdelegate=(AppDelegate *)[UIApplicationsharedApplication].delegate;
appdelegate.EDForceLandscapeRight = YES;
[appdelegate application:[UIApplication sharedApplication]supportedInterfaceOrientationsForWindow:self.view.window];
}
/**
Description 强制翻转屏幕
@param Orientation 方向
*/
- (void)mandatoryDeviceOrientation:(UIInterfaceOrientation)Orientation
{
[[UIDevice currentDevice] setValue:@([selfpreferredInterfaceOrientationForPresentation])forKey:@"orientation"];
}
/**
Description 刷新设备方向
*/
- (void)refreshRotationToDeviceOrientation
{
[UIViewController attemptRotationToDeviceOrientation];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;//只支持正常方向
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
在需要竖屏的VC里设置
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self forceOrientationPortrait];
[self mandatoryDeviceOrientation:[self preferredInterfaceOrientationForPresentation]];
}
/**
Description 强制竖屏
*/
- (void)forceOrientationPortrait
{
AppDelegate *appdelegate=(AppDelegate *)[UIApplicationsharedApplication].delegate;
appdelegate.EDForceLandscapeRight=false;
[appdelegate application:[UIApplication sharedApplication]supportedInterfaceOrientationsForWindow:self.view.window];
}
/**
Description 强制翻转屏幕
@param Orientation 方向
*/
- (void)mandatoryDeviceOrientation:(UIInterfaceOrientation)Orientation
{
[[UIDevice currentDevice] setValue:@([selfpreferredInterfaceOrientationForPresentation])forKey:@"orientation"];
}
/**
Description 刷新设备方向
*/
- (void)refreshRotationToDeviceOrientation
{
[UIViewController attemptRotationToDeviceOrientation];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
Appdelegate中设置
-(UIInterfaceOrientationMask)application:(UIApplication *)applicationsupportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.EDForceLandscapeRight)
{
returnUIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskPortrait;
}
info 中设置
测试效果
网上很多介绍下面的方法
-(void)interfaceOrientation:(UIInterfaceOrientation)orientation{
if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)])
{
SEL selector =NSSelectorFromString(@"setOrientation:");
NSInvocation*invocation =[NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];
[invocationsetSelector:selector];
[invocation setTarget:[UIDevicecurrentDevice]];
intval = orientation;
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocationsetArgument:&valatIndex:2];
[invocation invoke];
}
}
这个方法并不实用,而且在切换过程中会有你意想不到的意外惊喜。
总结:1 在转屏过程中 方向枚举值要一致。
2.调用顺序很重要
3. 方法可提炼