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个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...
随机推荐
- 【Android Developers Training】 8. 定义Action Bar风格
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Java 获取字符串Hash值
Java 生成字符串的Hash值: /** * A hashing method that changes a string (like a URL) into a hash suitable for ...
- h5 新增的invalid事件,貌似有很大bug
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 如何将md文件转换成带目录的html文件
配置环境node 去官网下一个node安装包,下一步下一步: 由于现在的node都自带npm,直接 npm install i5ting_toc 这样安装好了i5ting_toc这个包, 进入你实现准 ...
- C#工作笔记
没想到一个Java后端开发还要负责C#桌面程序,我感觉有点方.不过方归方,活还是要干的.简单记录下学到的一些知识点. 1.引用API函数 namespace Demo { class MyUtil { ...
- Python之编写登陆接口
1.输入用户名密码: 2.认证成功后显示欢迎信息: 3.错误三次后,账号被锁定. 账号文件:user.txt 锁定文件:locked.txt 流程图如下: # -*- coding:utf-8 -*- ...
- java for循环
publicclass Test2 { staticboolean foo(char c) { System.out.print(c); returntrue; ...
- DDD理论学习系列(10)-- 聚合
DDD理论学习系列--案例及目录 1.引言 聚合,最初是UML类图中的概念,表示一种强的关联关系,是一种整体与部分的关系,且部分能够离开整体而独立存在,如车和轮胎. 在DDD中,聚合也可以用来表示整体 ...
- java 分页模型的模板
分页sql select top 每页要显示的记录数 * from 表名 where 主键 not in (select top (每页显示的记录数*(当前页-1)) 主键 from 表名 ) sel ...
- HDU 5616 Jam's balance(DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...