里脊串大神 --- 在开发过程中,有的页面需要横屏,有的不需要, 所以本文带来强制旋转屏幕
2015-12-23 11:35
246 查看
如何强制旋转屏幕
前言
如果做过项目中有视频播放的内容 一定遇到过全屏播放视频的需求 一般来说 直接弹出一个modal viewcontroller来全屏播放视频就ok了但是为了用户体验 可能需要在同一个viewcontroller中进行全屏和非全屏的切换 这个时候可能就需要强制旋转屏幕来做到
那么如何完美的对屏幕进行强制旋转呢?
这里只讨论ios6+的方法(因为从ios6+起 屏幕旋转的api做了重要的变化 有需要的同学可以自己去探索一下ios6-的方法)
研究
首先 你得在项目的的plist中或者target的general选项里配置好APP所支持的所有放心啊个然后 在ios6+之后 屏幕旋转的控制权交给了rootViewController(这里没探讨presentViewController的情况 只考虑普通的布局) 那么基本上所有的APP都是基于
UINavigationController或者
UITabbarViewController做为rootViewController的
所以要灵活的控制每个push进来的view的旋转 需要修改一下
UINavigationController或者
UITabbarViewController的以下几个方法(可以添加Category
也可以继承一下)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | //UINavigationController - (BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } - (NSUInteger)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return self.topViewController.preferredInterfaceOrientationForPresentation; } //UITabbarViewController //注意 这里的selectedViewController如果是UINavigationController的话 也需要对它做上面的修改 - (BOOL)shouldAutorotate { return self.selectedViewController.shouldAutorotate; } - (NSUInteger)supportedInterfaceOrientations { return self.selectedViewController.supportedInterfaceOrientations; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return self.selectedViewController.preferredInterfaceOrientationForPresentation; } |
方法
在ios6中 使用如下的trick可以强制让屏幕进行旋转检测1 2 3 4 | UIViewController *vc = [[UIViewController alloc]init]; [self presentModalViewController:vc animated:NO]; [self dismissModalViewControllerAnimated:NO]; |
1 2 3 4 | UIViewController *vc = [[UIViewController alloc] init]; [self presentViewController:viewController animated:NO completion:^{ [self dismissViewControllerAnimated:NO completion:nil]; }]; |
实际上有个更好的方法
1 | [[UIDevice currentDevice] setValue: [NSNumber numberWithInteger: self.isFullScreen?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait] forKey:@"orientation"]; //等价于 objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), self.isFullScreen?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait); |
但是
亲测上架无碍
相关文章推荐
- [Swift]之[与OC source file 混编]
- 《C专家编程》笔记——数组与指针
- Unity3D游戏内存瘦身 UI优化是关键
- oracle 日期函数大全
- C#之网络
- 随想18:论大学生
- sp_who使用
- PPT2007中的剪贴画怎样修改?ppt模板下载
- 单链表代码2
- swift学习日志—— 闭包(一)
- IOS开发-KVO详解
- 1028. 人口普查(20)
- ios调用其它App
- Java IO系列2 InputStream之ObjectInputStream
- 百例总结
- 10个大坑,当你产品上架AppStore会遇到
- Error: MDM failed command. Status: Only a single SDC may be mapped to this volume at a time
- android自定义seekBar
- 使用RMAN Active duplicate创建异地auxiliary Database
- Maven搭建Eclipse项目