iOS要实现横竖屏切换很简单,不需要使用任何第三方,只需要实现几个方法就可以了。demo下载地址:https://github.com/qqcc1388/TYOrientationDemo

1.设置系统支持横竖屏【General】->【Targets】-> 【Deployment info】->【Device Orientation】

2.在控制器中实现对应的方法(默认支持竖屏)

-(BOOL)shouldAutorotate{
return YES;
} -(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

3.某些特定的页面需要横屏支持的,则单独处理(同样需要实现2个方法)

-(BOOL)shouldAutorotate{
return YES;
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight);
}

4.如果控制器中有用到导航控制器或者tabbarController,这需要在导航控制器和tabbarController分别实现以下2个方法

- (BOOL)shouldAutorotate{

    return [self.selectedViewController shouldAutorotate];
} - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [self.selectedViewController supportedInterfaceOrientations];
}

5.如果需要实现点击按钮切换横竖屏的效果则可以参考如下方案:

//按钮点击事件
- (void)leftAction
{
[self interfaceOrientation:UIInterfaceOrientationPortrait];
} - (void)rightAction
{
[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
} - (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}

6.如果要监听屏幕旋转事件则可以参考如下方案(监听旋转事件,控制键盘,控制frame等操作)

    UIDevice *device = [UIDevice currentDevice]; //Get the device object
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device]; -(void)orientationChanged:(NSNotification *)noti{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation) {
case UIDeviceOrientationPortrait: // Device oriented
break;
case UIDeviceOrientationPortraitUpsideDown: // Device oriented
break;
case UIDeviceOrientationLandscapeLeft : // Device oriented
break;
case UIDeviceOrientationLandscapeRight: // Device oriented horizontally, home button on the left
break;
default:
break;
}
}

iOS 横竖屏切换解决方案的更多相关文章

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

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

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

    Android横竖屏切换解决方案 首先在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidde ...

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

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

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

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

  5. 【IOS界面布局】横竖屏切换和控件自适应(推荐)

    [IOS界面布局]横竖屏切换和控件自适应(推荐) 分类: [MAC/IOS下开发]2013-11-06 15:14 8798人阅读 评论(0) 收藏 举报 横竖屏切换 自适应 第一种:通过人为的办法改 ...

  6. iOS 中各种横竖屏切换总结

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

  7. iOS开发之判断横竖屏切换

    /** *  当屏幕即将旋转的时候调用 * *  @param toInterfaceOrientation 旋转完毕后的最终方向 *  @param duration  旋转动画所花费的时间 */ ...

  8. Activity 横竖屏切换

    前言 在开发中常要处理横竖屏切换,怎么处理先看生命周期 申明 Activity 横竖屏切换时需要回调两个函数 ,所以在此将这个两个函数暂时看成是Activity 横竖屏切换的生命周期的一部分,这两个函 ...

  9. Android应用:横竖屏切换总结

    眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...

随机推荐

  1. [leetcode-567-Permutation in String]

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  2. 【Android Developers Training】 101. 显示快速联系人挂件(Quick Contact Badge)

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. jq-animate

    jq-animate: <!doctype html> <html> <head> <meta charset="utf-8"> & ...

  4. 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件

    今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...

  5. Disruptor的应用示例——大文件拆分

    结合最近Disruptor的学习,和之前一直思考解决的大文件拆分问题,想到是否可以使用Disruptor作为生产者/消费者传递数据的通道呢?借助其高效的传递,理论上应当可以提升性能.此文便是此想法的落 ...

  6. Logback 基础知识

    1.Logback使用logback 是log4j的替代者,其需要slf4j.其配置文件主要有以下三种1. logback.groovy2. logback-test.xml3. logback.xm ...

  7. Django学习(九)---Templates过滤器及Django shell和Admin增强

    一.Templates过滤器 过滤器属于django模板语言 修改模板中的变量,从而显示不同内容 {{ value | filter }} 举例:{{ list_nums | length}}    ...

  8. 利用TinyXml进行数据库的热更新

    TinyXml库比较小,但功能较为完善,挺适合用来读取小块的xml文件; 我写了几个利用TinyXml读取和保存数据的例子,大家可以参考使用; 主要是为了热更新配置所做的一些函数应用; //开始热更 ...

  9. django MVC模式 数据库的操作mysql

    介绍:本节课我们继续学习djangoWEB框架的开发,这节课主要是学习如何访问数据库,django如何自动为我们创建好表结构等相关内容. 1.首先我们打开settings.py找到DATABASES关 ...

  10. QuartusII 13.0 PLL IP Core调用及仿真

    有一个多月没用用Quartus II了,都快忘了IP 是怎么用调用的了,还好有之前做的笔记,现在整理出来,终于体会到做笔记的好处. 一.  QuartusII的pll的调用 打开软件界面 Tool—— ...