iOS强制横屏或强制竖屏

第一种解决方案(不推荐,直接跳过看第二种解决方案):

需求:

强制横竖屏,在某些情况下非常重要,在网上找了好多解决的方法,都没找到解决方法,请原谅我有点笨哈,不过经过了一段纠结的时光,终于解决了这个问题,会出现无法转屏的情况!!!

//强制转屏

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{

if([[UIDevicecurrentDevice] respondsToSelector:@selector(setOrientation:)])

 {      

 SEL selector  =NSSelectorFromString(@"setOrientation:");NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];

 [invocation setSelector:selector];  

 [invocation setTarget:[UIDevicecurrentDevice]];

intval = orientation;// 从2开始是因为0 1 两个参数已经被selector和target占用[invocation setArgument:&val atIndex:2];     

[invocation invoke];    

}

}

强制横屏:

[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

强制竖屏:

[self interfaceOrientation:UIInterfaceOrientationPortrait];

总算是解决了这个头疼的问题,哈哈哈---啊---duang

只在某一个界面提供转屏的解决方法如下AppDelegate.m下操作

-(UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window {

NSLog(@"0000000---------%@",NSStringFromClass([[selftopViewController]class]));

if([NSStringFromClass([[selftopViewController]class]) isEqualToString:@"想要提供转屏的控制器的名字"]) {

//横屏returnUIInterfaceOrientationMaskLandscapeRight;    }//竖屏returnUIInterfaceOrientationMaskPortrait;}//获取界面最上层的控制器- (UIViewController*)topViewController {

return[self topViewControllerWithRootViewController:[UIApplicationsharedApplication].keyWindow.rootViewController];

}

//一层一层的进行查找判断

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController

{

if([rootViewController isKindOfClass:[UITabBarControllerclass]]) {

      UITabBarController* tabBarController = (UITabBarController*)rootViewController;             return[self topViewControllerWithRootViewController:tabBarController.selectedViewController]; 

 }

else if([rootViewController isKindOfClass:[UINavigationControllerclass]]) {

     UINavigationController* nav = (UINavigationController*)rootViewController;

      return[self topViewControllerWithRootViewController:nav.visibleViewController]; 

  }

else if(rootViewController.presentedViewController) {

       UIViewController* presentedViewController = rootViewController.presentedViewController;

        return [self topViewControllerWithRootViewController:presentedViewController];    }

else{

         return rootViewController; 

  }

}

2017.5.15更新:

在设置控制器的旋转屏的时候,我目前的应用中的处理方式:

因为在大部分的场景中,只有个别页面有横屏的情况,所以基本上都是竖屏,所以我在处理这两种情况的时候,优先选择使用系统提供的方法:

如果你的应用的根控制器是Nav就把下面这段代码放到Nav根控制器下,如果是TabVC放到TabVC的下面

- (BOOL)shouldAutorotate{returnYES;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

returnUIInterfaceOrientationMaskPortrait;

}

然后在你想横屏的控制器加上这段代码,基本上横屏问题就可以搞定了,前提是你的这个控制器是moda出来的,如果是push的话就要使用上文提到的强制横竖屏的方法,下面这段代码是不起作用的

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

return(toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight);

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

returnUIInterfaceOrientationMaskLandscapeRight;

}

第二种解决方案:

灵活设置横竖屏,不用区分Push还是Present,都是可以设置。

第一步:

在AppDelegate.h中添加旋转属性/**

* 是否允许转向*/

@property(nonatomic,assign)BOOLallowRotation;

在AppDelegate.m中添加转屏的代理方法

- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(nullableUIWindow*)window{

if(self.allowRotation ==YES) {

//横屏

returnUIInterfaceOrientationMaskLandscape;   

 }else{

//竖屏

returnUIInterfaceOrientationMaskPortrait;      

  }   

 }

第二步:

设置横竖屏的核心方法,我是直接把这个方法添加到了UIDevice的分类中,代码如下:

UIDevice+TFDevice.h :

#import@interfaceUIDevice(TFDevice)/**

* @interfaceOrientation 输入要强制转屏的方向

*/+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;@end

UIDevice+TFDevice.m :

#import"UIDevice+TFDevice.h"

@implementationUIDevice(TFDevice)

+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation{

NSNumber*resetOrientationTarget = [NSNumbernumberWithInt:UIInterfaceOrientationUnknown];     [[UIDevicecurrentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

NSNumber*orientationTarget = [NSNumbernumberWithInt:interfaceOrientation];                [[UIDevicecurrentDevice] setValue:orientationTarget forKey:@"orientation"]; 

 }

@end

第三步:

在需要设置横屏的控制器的ViewDidLoad中添加下面代码:

- (void)viewDidLoad {   

[superviewDidLoad]; 

  AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;//允许转成横屏

appDelegate.allowRotation =YES;//调用横屏代码[UIDeviceswitchNewOrientation:UIInterfaceOrientationLandscapeRight];

}

第四步 (针对Push出的控制器来说):

需要注意的是push过去的时候变成横屏,pop出去的时候在设置竖屏,此时最好禁用系统的侧滑返回手势。

-(void)viewWillAppear:(BOOL)animated{    [superviewWillAppear:animated];//禁用侧滑手势方法self.navigationController.interactivePopGestureRecognizer.enabled =NO;}-(void)viewWillDisappear:(BOOL)animated{    [superviewWillDisappear:animated];//禁用侧滑手势方法self.navigationController.interactivePopGestureRecognizer.enabled =YES;}

第五步:

push控制器:

//点击导航栏返回按钮的时候调用,所以Push出的控制器最好禁用侧滑手势:AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;    appDelegate.allowRotation =NO;//关闭横屏仅允许竖屏//切换到竖屏[UIDeviceswitchNewOrientation:UIInterfaceOrientationPortrait];            [self.navigationController popViewControllerAnimated:YES];

present控制器:

AppDelegate * appDelegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;    appDelegate.allowRotation =NO;//关闭横屏仅允许竖屏//切换到竖屏[UIDeviceswitchNewOrientation:UIInterfaceOrientationPortrait];        [selfdismissViewControllerAnimated:YEScompletion:nil];

第六步:

Demo地址:https://github.com/aiyakuaile/PLTest

你可能感兴趣的:(iOS强制横屏或强制竖屏)