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的方向,需要哪个 ...
随机推荐
- 2014-10-5 NOIP模拟赛
祖孙询问 (tree.pas/c/cpp) [问题描述] 已知一棵n个节点的有根树.有m个询问.每个询问给出了一对节点的编号x和y,询问x与y的祖孙关系. [输入格式] 输入第一行包括一个整数n表示节 ...
- Unity 播放的声音比声音文件小很多-AudioListener-AudioClip
今天做愤怒的小鸟时,播放的时候非常非常小,怎么也查不到原因,就去问群里的大佬.原来, 播放音乐的方法: AudioSource.PlayClipAtPoint(audioclip, transform ...
- [BeiJing wc2012]连连看
题目链接 费用流板子+拆点 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int r ...
- PostgreSQL - invalid input syntax for type timestamp with time zone
问题 在执行以下sql时报错: select COALESCE(null,null,now(),''); 报错如下: SQL Error [22007]: ERROR: invalid input s ...
- csdn自动展开+去广告+净化剪切板+免登陆(如有侵权,立即删博)
对于csdn的广告大家想必......又没钱充VIP,怎么办,下面是脚本源码: 重要的事说三遍:如有侵权,立即删除!如有侵权,立即删除!如有侵权,立即删除! // ==UserScript== // ...
- 练习三十二:用python实现:按相反的顺序输出列表的每一位值
用python实现:按相反的顺序输出列表的每一位值 1. 使用list[::-1] list1 = ["one","two","three" ...
- 过流监测芯片ADS720/723
在电机应用领域经常需要用到过流监测和保护,allegro的ADS系列就可以很好实现.将芯片串接在电机之前,根据自己要保护的电流大小选择合适的量程,个根据自己ADC测量电压范围选择合适的灵敏度.这类芯片 ...
- node-amqp 使用fanout发布订阅rabbitmq消息
publisher代码 const amqp = require('amqp'); let option = { host: 'server-ip', port: 5672, login: 'gues ...
- feign hystrix加仪表盘
Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数 ...
- Kendo MVVM 数据绑定(十) Source
Kendo MVVM 数据绑定(十) Source Source 绑定可以把 ViewModel 的值和由 Kendo 模板定义的目标元素绑定,如果 ViewModel 的值发生变化,被绑定的目标元素 ...