原文链接 https://www.jianshu.com/p/d6cb54d2eaa1

亲测第二种我这边是阔以滴

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

//强制转屏
- (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];
}
}

强制横屏:

[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

强制竖屏:

[self interfaceOrientation:UIInterfaceOrientationPortrait];

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

-(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的下面

- (BOOL)shouldAutorotate{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

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

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}

第二种解决方案:

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

第一步:

在AppDelegate.h中添加旋转属性

/**
* 是否允许转向
*/
@property(nonatomic,assign)BOOL allowRotation;
在AppDelegate.m中添加转屏的代理方法

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

{

    if (self.allowRotation == YES) {
//横屏
return UIInterfaceOrientationMaskLandscape; }else{
//竖屏
return UIInterfaceOrientationMaskPortrait; } }

第二步:

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

  • UIDevice+TFDevice.h :
#import <UIKit/UIKit.h>
@interface UIDevice (TFDevice)
/**
* @interfaceOrientation 输入要强制转屏的方向
*/
+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end
  • UIDevice+TFDevice.m :
#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中添加下面代码:

- (void)viewDidLoad {
[super viewDidLoad]; AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//允许转成横屏
appDelegate.allowRotation = YES;
//调用横屏代码
[UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];
}

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

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

-(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控制器:

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

present控制器:

AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏
//切换到竖屏
[UIDevice switchNewOrientation:UIInterfaceOrientationPortrait]; [self dismissViewControllerAnimated:YES completion:nil];

第六步: 上图

iOS强制横屏或强制竖屏的更多相关文章

  1. iOS的横屏(Landscape)与竖屏(Portrait)InterfaceOrientation

    http://www.molotang.com/articles/1530.html 接着上篇写的触摸事件,这次借机会整理下iOS横屏和竖屏的翻转方向支持,即InterfaceOrientation相 ...

  2. Android开发中如何强制横屏和强制竖屏设置

    Android开发中如何强制横屏和强制竖屏设置 强制横屏设置: 按照下面代码示例修改Activity的onResume方法 @Override protected void onResume() { ...

  3. 二维码识别项目zxing横屏改为竖屏

    第1步: 在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改: android:screenOrientation="por ...

  4. iOS 工程默认只允许竖屏,在单独界面进行横竖转换,屏幕旋转

    只含有 .关于横竖屏的代码 #import "InspectionReportViewController.h" #define SCREEN_WIDTH ([UIScreen m ...

  5. 单个ViewController支持横屏,其他全竖屏方法-b

    如果项目中用了navigationViewController, 那么就应该新建一个uinavigationViewController的子类,然后在这个类里面写上下面的代码,在使用的时候就用自定义的 ...

  6. iOS应用开发,全局强制竖屏,部分页面允许旋转的处理

    目前大多数app都不支持横屏模式,用户不常用,开发起来也麻烦一些.但有些时候,又离不开横屏和竖屏的切换,比如查看视频.图片.文档等,这时又不得不对页面做横屏的处理.下面来教大家如何处理这种场景,方法可 ...

  7. iOS强制横屏

    由于项目需求,需要整个项目页面都是竖屏,唯独一个折线图页面强制性横屏显示. 网上逛了许多帖子,也看了好多大神的提供的方法,都没能够实现本屌丝想要的效果.没办法自己研究自己搞,借鉴各路大神的思路,最后费 ...

  8. js判断手机浏览器是横屏or竖屏

    移动端的浏览器一般都支持window.orientation这个参数,通过这个参数可以判断出手机是处在横屏还是竖屏状态. 从而根据实际需求而执行相应的程序.通过添加监听事件onorientationc ...

  9. 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏

    function orient() {if (window.orientation == 90 || window.orientation == -90) {//ipad.iphone竖屏:Andri ...

随机推荐

  1. 潭州课堂25班:Ph201805201 django 项目 第三十八课 后台 文章发布,FastDFS安装 配置(课堂笔记)

    , .安装FastDFS # 从docker hub中拉取fastdfs镜像docker pull youkou1/fastdfs # 查看镜像是否拉取成功docker images # 安装trac ...

  2. 2017中国无人机公开赛 总决赛 CDR Final 竞赛规则

    2017中国无人机公开赛总决赛 CDR Final竞赛规则 V2 二〇一七年八月(修订) 一. 竞赛项目描述(一) 无人机绕标竞速赛(专业组)飞行员通过佩戴眼镜或显示屏采用第一视角飞行,使用无线电遥控 ...

  3. 考前停课集训 Day3 匪

    Day3——作死不可活的一天 Day3 今天下午才考 晚上时间少 下午网每断 因此我是PY的 然后被抓了 成绩做0分处理. 是啊,我只会抄题解. 其他什么都不会. 一无是处. 真的. 真实能力:ran ...

  4. [P1373]小a和uim之大逃离 (DP)

    [题目链接] 模拟赛的时候的一道题 因为老师不小心把数据发下来了……我考试打表的 考完之后Orz xzjds 然后开始打正解 题意 大概就是两个人,走矩阵,两个人各加上走上的矩阵的数值,要求最终两个人 ...

  5. 3ds max学习笔记(五)--操作工具

    一些快捷:移动,旋转,缩放右键可调整数据或者直接右键点开也可以看到或者快捷键W,E,R 复制物体:摁住shift+移动工具,往想要复制的位置拖拽(实例方式复制)亦可选择多个父对象

  6. ArrayList源码分析和实例应用

    1.ArrayList介绍 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAcces ...

  7. pycharm实现sublime的显示效果,很惊艳哦

    收到https://github.com/simoncos/pycharm-monokai链接中的指引 下载箭头所指的文件,然后按照 PyCharm -> File -> Settings ...

  8. 牛客网测试题--小a和黄金街道

    小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们拿走的太多,于是要求小a和小b通过做一个游戏来决定最后得到的黄金的数量.游戏规则是这样的:假设道路长度为米(左端点 ...

  9. CSS_对齐

    2016-10-25 <css入门经典>第15章 1.text-align属性: 块属性内部的文本对齐方式.该属性只对块盒子有意义,内联盒子的内容没有对齐方式.(注意:只是盒子内部的内容对 ...

  10. js的几个补充事件

    在这里我做几个前面文章当中没有介绍的javascript补充事件 1.onscroll:当元素滚动条滚动时执行的事件: <div class="container"> ...