现在开发的APP大部分界面是竖屏的,只有视频播放的界面和webview阅读文字的界面是可以横屏操作的。

那么就进行如下处理:

  1、首先确保APP支持横屏旋转

  2、我的App里面都是走UINavigationController进行界面push切换的,所以首先创建一个UINavigationController的子类,并设定允许转屏:  

  

#pragma mark 转屏方法重写
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}
-(BOOL)shouldAutorotate{
return self.visibleViewController.shouldAutorotate;
}

在不想转屏切换的ViewController上重写以下方法:

  

#pragma mark 转屏方法 不允许转屏
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait ;
}
- (BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return NO;
}

在想转屏切换的ViewController上可以照这样重写(允许左右横屏以及竖屏):

  

- (BOOL)shouldAutorotate {
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

另外,在ViewController中对于转屏事件可以参见下面的方法进行捕获:

  

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
//计算旋转之后的宽度并赋值
CGSize screen = [UIScreen mainScreen].bounds.size;
//界面处理逻辑
self.lineChartView.frame = CGRectMake(, , screen.width, 200.0);
//动画播放完成之后
if(screen.width > screen.height){
NSLog(@"横屏");
}else{
NSLog(@"竖屏");
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
NSLog(@"动画播放完之后处理");
}];
}

区分当前屏幕是否为横竖屏的状态,其实通过判断当前屏幕的宽高来决定是不是横屏或者竖屏:

竖屏时:宽<高

横屏时:宽>高

以上在IOS8、9中测试通过

iOS横竖屏的更多相关文章

  1. iOS 横竖屏切换(应对特殊需求)

    iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...

  2. iOS 横竖屏切换解决方案

    iOS要实现横竖屏切换很简单,不需要使用任何第三方,只需要实现几个方法就可以了. 1.设置系统支持横竖屏[General]->[Targets]-> [Deployment info]-& ...

  3. iOS 横竖屏适配

    关于横竖屏适配 也没做过,今天读别人的源码,遇到了.为了了解清楚,就系统的学习一下. 一 横竖屏方向枚举 关于横竖屏一共有三种枚举 UIInterfaceOrientation UIInterface ...

  4. ios 横竖屏通知

    屏幕切换时,会发送一个通知.只要注册一个通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(do ...

  5. IOS横竖屏控制与事件处理

    公司App里面有个需求,即所有界面都是竖屏,且不允许横屏切换,唯独有一个图表界面允许横屏.那么,根据此需求处理如下: 首先,确保App本身应该允许转屏切换: 再次,我的App里面都是走UINaviga ...

  6. iOS横竖屏切换的一些坑(持续更新)

    最近在做视频类的App,遇到视频滚动播放的坑,紧接着就是横竖屏问题.之前太过天真不想做横竖屏配置.只是想旋转视频View,但是分享什么的包括AlertView还是竖屏样式,项目着急上线(1周提交一次也 ...

  7. iOS 横竖屏适配 笔记

    研究消息转发机制 已经一周多了,但是 还是没整理出博客, 还是先写一个 项目中遇到的 横竖屏适配问题. // 开启自动转屏 - (BOOL)shouldAutorotate { return YES; ...

  8. [iOS]终极横竖屏切换解决方案

    [iOS]终极横竖屏切换解决方案 大家的项目都是只支持竖屏的吧?大多数朋友(这其中当然也包括博主),都没有做过横屏开发,这次项目刚好有这个需求,因此把横竖屏相关的心得写成一遍文章供诸位参考. 01.综 ...

  9. 代码处理 iOS 的横竖屏旋转

    一.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation.UIInterfaceOrientation和UIInterfaceOrientationMask这三个枚举 ...

随机推荐

  1. poj1988 简单并查集

    B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:30000KB     64bit ...

  2. 兄弟选择器 E + F

    兄弟选择器在IE7下支持会有bug,特记于此 如果兄弟选择器有Html注释,兄弟选择器在IE7下会失效  代码如下 E + Fp + p{color:red} <p class="te ...

  3. zepto自定义事件

    <!--测试自定义事件 trigger--> <script> $(function () { var meEvent = $.Event("custom" ...

  4. sql server在使用xp_cmdshell

    一.sql server在使用xp_cmdshell读取远程服务器上的文件时,要先将远程服务器的目录映射到本地 代码: exec master..xp_cmdshell  'net use P: \\ ...

  5. PHP限制网页只能在微信内置浏览器中查看并显示

    微信现在算是火了,围绕微信开发的应用也越来越多了,前段时间,自己公司需要,用PHP写了一个微信应用,为了防止自己辛苦写成的PHP应用被盗用,于是 通过PHP做了限制,只能在微信自带的浏览器中才能打开本 ...

  6. BufferedInputStream

    package file; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStrea ...

  7. MySQL性能分析脚本

    #!/usr/bin/python #!coding:utf-8 import mysql.connector as connector import json """ ...

  8. 采购IC应该知道的十大网站

    http://www.hcsindex.com  (华强北指数网,查价格的)http://www.hqew.com  (华强电子网,针对华强北市场)http://www.dzsc.com  (维库电子 ...

  9. Ruby中的Symbol与字符串

    刚开始学Ruby,一下子搞不清其中的Symbol(变量需要加:)和字符串有什么区别,为这么要为语言设计这么一个东西.让我很迷惑. 首先,字符串对象,是不同的.比如"String" ...

  10. android中button点击频率控制

    public class Utils { private static long lastClickTime; public static boolean isFastDoubleClick() { ...