里脊串大神 --- 在开发过程中,有的页面需要横屏,有的不需要, 所以本文带来强制旋转屏幕
来源:互联网 发布:机房网络拓扑结标注 编辑:程序博客网 时间:2024/04/27 18:35
如何强制旋转屏幕
前言
如果做过项目中有视频播放的内容 一定遇到过全屏播放视频的需求 一般来说 直接弹出一个modal viewcontroller来全屏播放视频就ok了
但是为了用户体验 可能需要在同一个viewcontroller中进行全屏和非全屏的切换 这个时候可能就需要强制旋转屏幕来做到
那么如何完美的对屏幕进行强制旋转呢?
这里只讨论ios6+的方法(因为从ios6+起 屏幕旋转的api做了重要的变化 有需要的同学可以自己去探索一下ios6-的方法)
研究
首先 你得在项目的的plist中或者target的general选项里配置好APP所支持的所有放心啊个
然后 在ios6+之后 屏幕旋转的控制权交给了rootViewController(这里没探讨presentViewController的情况 只考虑普通的布局) 那么基本上所有的APP都是基于UINavigationController
或者UITabbarViewController
做为rootViewController的 所以要灵活的控制每个push进来的view的旋转 需要修改一下UINavigationController
或者UITabbarViewController
的以下几个方法(可以添加Category 也可以继承一下)
1234567891011121314151617181920212223242526272829303132333435
//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;}
这样 控制旋转的主动权就交还到了每个viewController的手上了
方法
在ios6中 使用如下的trick可以强制让屏幕进行旋转检测
1234
UIViewController *vc = [[UIViewController alloc]init];[self presentModalViewController:vc animated:NO];[self dismissModalViewControllerAnimated:NO];
在ios7中 上面的方法不管用了 但是稍微改一改就可以了
1234
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);
在stackoverflow上看到有人说 此方法是私有方法 调用有可能会导致被reject
但是亲测上架无碍
- 里脊串大神 --- 在开发过程中,有的页面需要横屏,有的不需要, 所以本文带来强制旋转屏幕
- 页面有的参数,在action中没有报错
- iOS开发中控制屏幕旋转,个别界面强制横屏
- 在已有的方法中抽取接口
- 解答树后项添加的过程中因为存在分支,所以你想的一个排列不一定在你要的分支路径上,虽然已有的排列可能满足条件Uva129
- 每个人都在成为大神的路上,只不过有的人在走,而有的人在跑。
- 在已有的系统上做重构需要注意的几点
- .net中,插入flash 同一页面有的显示,有的不显示的解决方法
- UIWebView中视频播放屏幕自动旋转,app不支持旋转但是某一个页面需要旋转等
- UIWebView中视频播放屏幕自动旋转,app不支持旋转但是某一个页面需要旋转等
- 有的需要知其大略,有的需要务于精纯
- jsp页面下载java工程中已经有的excel模板
- java 在已有的学生中插入一个学生(需要用类去创建另一个类的属性)
- 写代码过程中必须有的几个本能
- sql 存储过程中 如何使用declare 有的存储过程声明了,有的没有声明。请回答的详细点
- iOS开发中,单个页面屏幕旋转问题
- iOS 指定页面允许旋转/ 指定页面强制横屏
- ueditor有的机器显示有的机器页面空白--解决
- 百例总结
- swift学习日志—— 闭包(一)
- 1028. 人口普查(20)
- swift的学习笔记(一)
- 10个大坑,当你产品上架AppStore会遇到
- 里脊串大神 --- 在开发过程中,有的页面需要横屏,有的不需要, 所以本文带来强制旋转屏幕
- Unity3D游戏内存瘦身 UI优化是关键
- Virtualenv
- sql语句的内外左右连接(left join and right join)
- oracle 日期函数大全
- [Swift]之[与OC source file 混编]
- 1029. 旧键盘(20)
- 235. Lowest Common Ancestor of a Binary Search Tree
- 《C专家编程》笔记——数组与指针