方法一,通过控制器继承或分类实现:

在UITabBarController 的子类或分类中实现

 - (BOOL)shouldAutorotate {
return [self.selectedViewController shouldAutorotate];
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.selectedViewController supportedInterfaceOrientations];
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

在UINavigationController 的子类或分类中实现

 - (BOOL)shouldAutorotate {
return [self.topViewController shouldAutorotate];
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.topViewController preferredInterfaceOrientationForPresentation];
}

在 UIViewController 的子类或分类中实现

 - (BOOL)shouldAutorotate {
// 是否允许转屏
return NO;
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
// 所支持的全部旋转方向
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// 初始显示的方向
return UIInterfaceOrientationPortrait;
}

最后在需要改变方向的控制器中重写以下方法实现可旋转功能

 - (BOOL)shouldAutorotate {
// 是否允许转屏
return YES;
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
// 所支持的全部旋转方向
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// 初始显示的方向
return UIInterfaceOrientationPortrait;
}

亲测可用

方法二:通过AppDelegate代理方法实现

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

     if ([[self topViewController] isKindOfClass:[MyInterfaceController class]]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;//竖屏
} // 获取当前window的顶层控制器
- (UIViewController *)topViewController {
UIViewController *resultVC;
resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
while (resultVC.presentedViewController) {
resultVC = [self _topViewController:resultVC.presentedViewController];
}
return resultVC;
} - (UIViewController *)_topViewController:(UIViewController *)vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self _topViewController:[(UINavigationController *)vc topViewController]];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
} else {
return vc;
}
return nil;
}

MyInterfaceController 为需要改变旋转方式的控制器

当从旋转过的屏幕返回不需要旋转的控制器时,需要强制旋转为指定方向,

- (void)viewWillAppear:(BOOL)animated 中调用:

 // 强制旋转屏幕
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
SEL selector = NSSelectorFromString(@"setOrientation:");
if ([[UIDevice currentDevice] respondsToSelector:selector]) {
// NSInvocation中保存了方法所属的对象/方法名称/参数/返回值
// 其实NSInvocation就是将一个方法变成一个对象
// 1.创建NSInvocation对象,设置方法签名
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
// 2.设置方法调用者
[invocation setTarget:[UIDevice currentDevice]];
// 3.写入签名方法
[invocation setSelector:selector];
// 4.设置参数
int val = orientation;
[invocation setArgument:&val atIndex:]; // 设置索引2或更大(如果签名方法再有一个参数测设置3来进行索引传递); 0,1参数为target和selector
// 开始执行
[invocation invoke];
}
}

完毕 ,亲测可用

iOS 设置随意屏幕旋转的更多相关文章

  1. iOS 设置系统屏幕亮度

    // 设置系统屏幕亮度    //    [UIScreen mainScreen].brightness = value;    // 或者    [[UIScreen mainScreen] se ...

  2. 处理iOS设备的屏幕旋转

    某些情况下,不强制的给用户唯一的屏幕角度给用户.这样用户可以旋转手机得到不同的视觉体验. 最简单的就是safari,横看竖看都可以. 这时需要捕捉用户的屏幕旋转事件并处理.很简单,才两步.比把大象装冰 ...

  3. ios 不支持屏幕旋转

    - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }

  4. iOS 中的屏幕旋转shouldAutorotate和supportedInterfaceOrientations的先后关系

    这2个UIViewController的属性,都和旋转相关, 当设备发生旋转时,首先会查看根controller的shouldAutorotate是否允许旋转,如果允许,再通过 supportedIn ...

  5. ios 单个ViewController屏幕旋转

    如果需要旋转的ViewController 使用了UINavigationController,对UINavigationController进行如下扩展 @implementation UINavi ...

  6. iOS开发——检测屏幕旋转

    步骤一.注册通知 1: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrien ...

  7. iOS学习笔记(3)— 屏幕旋转

    一.屏幕旋转机制: iOS通过加速计判断当前的设备方向和屏幕旋转.当加速计检测到方向变化的时候,屏幕旋转的流程如下: 1.设备旋转时,系统接收到旋转事件. 2.系统将旋转事件通过AppDelegate ...

  8. IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)

    转自 http://blog.csdn.net/zzfsuiye/article/details/8251060 概述: 在iOS6之前的版本中,通常使用 shouldAutorotateToInte ...

  9. iPhone屏幕旋转

    iPhone屏幕内容旋转 在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个 ...

随机推荐

  1. Codevs 1688 求逆序对(权值线段树)

    1688 求逆序对  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给定一个序列a1,a2,…, ...

  2. Luogu P3941 入阵曲【前缀和】By cellur925

    题目传送门 题目大意:给你一个\(n\)*\(m\)的矩阵,每个位置都有一个数,求有多少不同的子矩阵使得矩阵内所有数的和是\(k\)的倍数. 数据范围给的非常友好233,期望得到的暴力分:75分.前1 ...

  3. Linux 中 ip netns 命令

    通过 ip netns help 可以查看所有关于ip netns的命令: network namespace 在逻辑上是网络堆栈的一个副本,它有自己的路由.防火墙规则和网络设备. ip netns ...

  4. JDK 简介

    JDK简介 JDK java开发工具包 JRE java 运行时环境 JVM java虚拟机 三者的关系:JDK 包含 JRE,JRE 包含 JVM Java的核心优势是跨平台,由JVM虚拟机实现的. ...

  5. vue中的导航守卫

    官方文档地址: 导航守卫:https://router.vuejs.org/zh-cn/advanced/navigation-guards.html 好的,重点内容 router.beforeEac ...

  6. CC17:猫狗收容所

    题目 有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,第一种为直接收养所有动物中最早进入收容所的,第二种为选择收养的动物类型(猫或狗),并收养该种动物中最早进入收容所的. 给定一 ...

  7. 6.计算字段 ---SQL

    提示:客户端与服务器的格式在SQL语句内可完成的许多转换和格式化工作都可以直接在客户端应用程序内完成.但一般来说,在数据库服务器上完成这些操作比在客户端中完成要快得多. 一.拼接字段 拼接(conca ...

  8. [Android基础]Android四大组件之Activity总结

    1.Activity简介 Activity是Android一个非常重要的用户接口(四大组件之一),是可见的,主要是用户和应用程序之间进行交互的接口.在每个Activity中都可以放很多控件,所以也可以 ...

  9. [Java]基本数据类型及其封装类总结

    九种基本数据类型的大小,以及他们的封装类 类型 字节 默认值 封装类 byte 1 0 Byte char 2 null Character int 4 0 Integer long 8 0 Long ...

  10. Codeforces 161E(搜索)

    要点 标签是dp但搜索一发就能过了. 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5. 显然要预处理有哪些素数.在这个过程中可以顺便再处理出一个\(vector:re[le ...