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

在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];
}
}

完毕 ,亲测可用

最新文章

  1. 远程执行shellcode
  2. mysql中的SUBSTRING_INDEX
  3. VMware 搭建 虚拟机设置 静态IP地址配置
  4. 创建Maven项目
  5. linux 屏幕亮度调整命令
  6. HTML&CSS基础学习笔记1.5-添加常用标签
  7. 数据库数据用Excel导出的3种方法
  8. 能量项链AC了
  9. JavaScript学习总结(十七)——Javascript原型链的原理
  10. poj1014二进制优化多重背包
  11. SpringCloud的服务注册中心(二)注册中心服务端和两个微服务应用客户端
  12. Get Form type using javascript in CRM 2011
  13. 《深入java虚拟机》读书笔记之垃圾收集器与内存分配策略
  14. 一篇文章彻底弄懂Base64编码原理
  15. QT插件+ROS 3 导入现有ROS包
  16. 笔记react router 4(五)
  17. Python 深度递归
  18. mysql中min和max查询优化
  19. 【洛谷】【单调队列】P2032 扫描
  20. C语言 IPv6 十六进制 转 十进制

热门文章

  1. 剑指Offer的学习笔记(C#篇)-- 链表中倒数第K个点
  2. UVA - 1330 City Game
  3. 从各处收集的switch语句
  4. Django之Form的ModelForm
  5. Codeforces 126B(kmp)
  6. 矩形面积并-扫描线 线段树 离散化 模板-poj1151 hdu1542
  7. GPIO的翻转操作方法
  8. MapReduce基本流程与设计思想初步
  9. Tensorflow版Faster RCNN源码解析(TFFRCNN) (2)推断(测试)过程不使用RPN时代码运行流程
  10. Kendo MVVM 数据绑定(三) Click