iOS 设置随意屏幕旋转
方法一,通过控制器继承或分类实现:
在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 设置随意屏幕旋转的更多相关文章
- iOS 设置系统屏幕亮度
// 设置系统屏幕亮度 // [UIScreen mainScreen].brightness = value; // 或者 [[UIScreen mainScreen] se ...
- 处理iOS设备的屏幕旋转
某些情况下,不强制的给用户唯一的屏幕角度给用户.这样用户可以旋转手机得到不同的视觉体验. 最简单的就是safari,横看竖看都可以. 这时需要捕捉用户的屏幕旋转事件并处理.很简单,才两步.比把大象装冰 ...
- ios 不支持屏幕旋转
- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
- iOS 中的屏幕旋转shouldAutorotate和supportedInterfaceOrientations的先后关系
这2个UIViewController的属性,都和旋转相关, 当设备发生旋转时,首先会查看根controller的shouldAutorotate是否允许旋转,如果允许,再通过 supportedIn ...
- ios 单个ViewController屏幕旋转
如果需要旋转的ViewController 使用了UINavigationController,对UINavigationController进行如下扩展 @implementation UINavi ...
- iOS开发——检测屏幕旋转
步骤一.注册通知 1: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrien ...
- iOS学习笔记(3)— 屏幕旋转
一.屏幕旋转机制: iOS通过加速计判断当前的设备方向和屏幕旋转.当加速计检测到方向变化的时候,屏幕旋转的流程如下: 1.设备旋转时,系统接收到旋转事件. 2.系统将旋转事件通过AppDelegate ...
- IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
转自 http://blog.csdn.net/zzfsuiye/article/details/8251060 概述: 在iOS6之前的版本中,通常使用 shouldAutorotateToInte ...
- iPhone屏幕旋转
iPhone屏幕内容旋转 在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个 ...
随机推荐
- MySQL变更之:Online DDL 和 PT-OSC 该选谁?
参考: http://www.fromdual.ch/online-ddl_vs_pt-online-schema-change 在MySQL 5.6版本以前,最昂贵的数据库操作之一就是执行数据定义语 ...
- [Xcode 实际操作]六、媒体与动画-(12)检测UIView动画的结束事件:反转动画并缩小至不可见状态
目录:[Swift]Xcode实际操作 本文将演示UIView视图反转动画的制作,并检测其动画结束事件. 即视图在进行反转动画的同时,将移动到目标位置,并缩小至不可见状态. 在项目导航区,打开视图控制 ...
- NOIp知识集合 By cellur925
基本算法 快速幂 ll ksm(ll a,ll b) { ll ans=; while(b) { ) ans=ans*a%p; b>>=; a=a*a%p; } return ans; } ...
- appium desktop连接模拟器
1.adb准备好,我建议,下载Androidstudio,因为这样adb是最新的,可能会避免很多问题 2.adb connect 127.0.0.1:xxxx (网易mumu是7555,别的模拟器自行 ...
- 基于apache httpclient的常用接口调用方法
现在的接口开发,大部分是基于http的请求和处理,现在整理了一份常用的调用方式工具类 package com.xh.oms.common.util; import java.io.BufferedRe ...
- 服务器配置,负载均衡时需配置MachineKey
服务器配置,负载均衡时需配置MachineKey https://blog.csdn.net/liuqiao0327/article/details/54018922 Asp.Net应用程序中为什么要 ...
- NET Core:部署项目到Ubuntu Server
NET Core:部署项目到Ubuntu Server 概述 基于上一篇成功安装Ubuntu Server 16.10的基础上,接下来继续我们ASP.NET Core项目的部署之旅! 只是对于这些年整 ...
- JSON.stringify 语法讲解
作用:这个函数的作用主要是为了系列化对象的. 可能有些人对系列化这个词过敏,我的理解很简单.就是说把原来是对象的类型转换成字符串类型(或者更确切的说是json类型的).就这么简单.打个比方说,你有一个 ...
- babel7中 corejs 和 corejs2 的区别
babel7中 corejs 和 corejs2 的区别 最近在给项目升级 webpack4 和 babel7,有一些改变但是变化不大.具体过程可以参考这篇文章 webpack4:连奏中的进化.只是文 ...
- springIOC+Mysql+springmvc事务测试题总结
1.关于http1.1和1.0的长连接和短连接 两个都支持长连接和短连接 http1.0默认为短连接,也就是说,浏览器和服务器每进行一次HTTP操作,就建立一次连接,任务结束就中断连接 http1.1 ...