我是靠谱客的博主 清脆硬币,这篇文章主要介绍iOS强制横屏或强制竖屏,现在分享给大家,希望可以做个参考。

原文链接 https://www.jianshu.com/p/d6cb54d2eaa1
亲测第二种我这边是阔以滴

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//强制转屏 - (void)interfaceOrientation:(UIInterfaceOrientation)orientation { if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; int val = orientation; // 从2开始是因为0 1 两个参数已经被selector和target占用 [invocation setArgument:&val atIndex:2]; [invocation invoke]; } }

强制横屏:

复制代码
1
[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

强制竖屏:

复制代码
1
[self interfaceOrientation:UIInterfaceOrientationPortrait];

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { NSLog(@"0000000---------%@",NSStringFromClass([[self topViewController] class])); if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"想要提供转屏的控制器的名字"]) { //横屏 return UIInterfaceOrientationMaskLandscapeRight; } //竖屏 return UIInterfaceOrientationMaskPortrait; } //获取界面最上层的控制器 - (UIViewController*)topViewController { return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; } //一层一层的进行查找判断 - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController { if ([rootViewController isKindOfClass:[UITabBarController class]]) { UITabBarController* tabBarController = (UITabBarController*)rootViewController; return [self topViewControllerWithRootViewController:tabBarController.selectedViewController]; } else if ([rootViewController isKindOfClass:[UINavigationController class]]) { UINavigationController* nav = (UINavigationController*)rootViewController; return [self topViewControllerWithRootViewController:nav.visibleViewController]; } else if (rootViewController.presentedViewController) { UIViewController* presentedViewController = rootViewController.presentedViewController; return [self topViewControllerWithRootViewController:presentedViewController]; } else { return rootViewController; } }

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

复制代码
1
2
3
4
5
6
- (BOOL)shouldAutorotate{ return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait; }

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

复制代码
1
2
3
4
5
6
7
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight; }

第二种解决方案:

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

第一步:

复制代码
1
2
3
4
5
6
在AppDelegate.h中添加旋转属性 /** * 是否允许转向 */ @property(nonatomic,assign)BOOL allowRotation;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
在AppDelegate.m中添加转屏的代理方法 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window { if (self.allowRotation == YES) { //横屏 return UIInterfaceOrientationMaskLandscape; }else{ //竖屏 return UIInterfaceOrientationMaskPortrait; } }

第二步:

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

  • UIDevice+TFDevice.h :
复制代码
1
2
3
4
5
6
7
#import <UIKit/UIKit.h> @interface UIDevice (TFDevice) /** * @interfaceOrientation 输入要强制转屏的方向 */ + (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation; @end
  • UIDevice+TFDevice.m :
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "UIDevice+TFDevice.h" @implementation UIDevice (TFDevice) + (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation { NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"]; NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"]; } @end

第三步:

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

复制代码
1
2
3
4
5
6
7
8
9
- (void)viewDidLoad { [super viewDidLoad]; AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; //允许转成横屏 appDelegate.allowRotation = YES; //调用横屏代码 [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight]; }

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

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

复制代码
1
2
3
4
5
6
7
8
9
10
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //禁用侧滑手势方法 self.navigationController.interactivePopGestureRecognizer.enabled = NO; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; //禁用侧滑手势方法 self.navigationController.interactivePopGestureRecognizer.enabled = YES; }

第五步:

push控制器:

复制代码
1
2
3
4
5
6
7
//点击导航栏返回按钮的时候调用,所以Push出的控制器最好禁用侧滑手势: AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏 //切换到竖屏 [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait]; [self.navigationController popViewControllerAnimated:YES];

present控制器:

复制代码
1
2
3
4
5
6
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏 //切换到竖屏 [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait]; [self dismissViewControllerAnimated:YES completion:nil];

第六步: 上图

762322-20180206085507654-1376820419.gif

最后

以上就是清脆硬币最近收集整理的关于iOS强制横屏或强制竖屏的全部内容,更多相关iOS强制横屏或强制竖屏内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部