iOS 横竖屏切换解决方案
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 横竖屏切换解决方案的更多相关文章
- [iOS]终极横竖屏切换解决方案
[iOS]终极横竖屏切换解决方案 大家的项目都是只支持竖屏的吧?大多数朋友(这其中当然也包括博主),都没有做过横屏开发,这次项目刚好有这个需求,因此把横竖屏相关的心得写成一遍文章供诸位参考. 01.综 ...
- Android横竖屏切换解决方案
Android横竖屏切换解决方案 首先在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidde ...
- iOS 横竖屏切换(应对特殊需求)
iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...
- iOS横竖屏切换的一些坑(持续更新)
最近在做视频类的App,遇到视频滚动播放的坑,紧接着就是横竖屏问题.之前太过天真不想做横竖屏配置.只是想旋转视频View,但是分享什么的包括AlertView还是竖屏样式,项目着急上线(1周提交一次也 ...
- 【IOS界面布局】横竖屏切换和控件自适应(推荐)
[IOS界面布局]横竖屏切换和控件自适应(推荐) 分类: [MAC/IOS下开发]2013-11-06 15:14 8798人阅读 评论(0) 收藏 举报 横竖屏切换 自适应 第一种:通过人为的办法改 ...
- iOS 中各种横竖屏切换总结
iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...
- iOS开发之判断横竖屏切换
/** * 当屏幕即将旋转的时候调用 * * @param toInterfaceOrientation 旋转完毕后的最终方向 * @param duration 旋转动画所花费的时间 */ ...
- Activity 横竖屏切换
前言 在开发中常要处理横竖屏切换,怎么处理先看生命周期 申明 Activity 横竖屏切换时需要回调两个函数 ,所以在此将这个两个函数暂时看成是Activity 横竖屏切换的生命周期的一部分,这两个函 ...
- Android应用:横竖屏切换总结
眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...
随机推荐
- ajax数据请求3(数组json格式)
ajax数据请求3(数组json格式) <!doctype html> <html> <head> <meta charset="utf-8&quo ...
- JS中关于clientWidth offsetWidth scrollWidth 的区别及意义
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- poj 2299 Ultra-QuickSort 题解
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- webpack教程(三)——热刷新
现在我们如果修改代码,需要重新打包,再一次在命令行下输入webpack命令,很麻烦. 热刷新是什么呢?就是我们该完代码保存之后webpack会自动打包引起浏览器自动刷新,你只需要把精力都专注在代码研发 ...
- JavaScript一个cookie存储的类
所有输出都在浏览器的控制台中 <script type="text/javascript"> /** * cookieStorage.js * 本类实现像localSt ...
- jvm 加载class文件过程
jvm 加载class文件分为装载-链接-初始化三个过程. load -------->link verify prepare resolve ---------->initial ...
- HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配)
HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配) Description The zoo have N cats and M dogs, toda ...
- DataReader To List
用了一段时间的Dapper,感觉Dapper比Ado.net好的地方就是转换成实体,到处查资料,写了以下方法,直接可以用. using (var conn = new SqlConnection(&q ...
- 输入三个整数x、y、z,请把这三个数由小到大输出
题目:输入三个整数x,y,z,请把这三个数由小到大输出. 程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x> y则将x与y的值进行交换,然后再用x与z进行比较,如果x> ...
- C++第三篇--程序结构
C++第三篇--程序结构 1. 初识程序结构 将类中的成员函数全部放在类外实现,类中只负责声明该函数 person.cpp #include <stdio.h> class Person{ ...