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

在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. LibreOJ #2036. 「SHOI2015」自动刷题机

    #2036. 「SHOI2015」自动刷题机 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 题目描述 曾经发明了信号增幅仪的发明家 SHTSC 又公开 ...

  2. 洛谷P3264 [JLOI2015]管道连接(斯坦纳树)

    传送门 感觉对斯坦纳树还是有很多疑惑啊…… 等到时候noip没有爆零的话再回来填坑好了 //minamoto #include<iostream> #include<cstdio&g ...

  3. 关于APICloud使用心得(原创)

    从最开始接触APICloud到现在已经有一段时间了.现在想说说自己对于APICloud开发移动端的想法,既有利又有弊. 以下都是我个人的观点. 先说优点吧: 1.APICloud平台文档.视频较多,很 ...

  4. [转] ios数组基本用法和排序

    http://blog.csdn.net/daiyelang/article/details/18726947 1.创建数组 // 创建一个空的数组 NSArray *array = [NSArray ...

  5. 关于idea的快捷键提示

    IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...

  6. Spark Mllib里如何程序输出数据集的条数(图文详解)

    不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第17章 决策树多元分类UCI Covertype数据集

  7. 物体检测丨从R-CNN到Mask R-CNN

    这篇blog是我刚入目标检测方向,导师发给我的文献导读,深入浅出总结了object detection two-stage流派Faster R-CNN的发展史,读起来非常有趣.我一直想翻译这篇博客,在 ...

  8. zeplin 登录效果实现

    zeplin 登录效果实现 zeplin 登录页有个效果不错,https://app.zeplin.io/login 可以看看. 主要是输入框的字会随着状态变化而变化. 我这里实现了一个自己的效果 实 ...

  9. Jquery3

    动画 动画动画效果一:show(时间),hide(时间)说明:时间的单位为毫秒方法toggle(时间):使用动画切换显示与隐藏动画效果二:slideDown(时间),slideUp(时间)切换:sli ...

  10. webpack.config.js====插件purifycss-webpack,提炼css文件

    1. 安装:打包编译时,可以删除一些html中没有使用的选择器,如果html页面中没有class=a class="b"的元素,.a{}.b{}样式不会加载 cnpm instal ...