iOS 中横竖屏切换
iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结。
注意
UIInterfaceOrientationLandscapeRight
与UIInterfaceOrientationMaskLandscapeRight
都代表横屏,Home键在右侧的情况;UIDeviceOrientationLandscapeLeft
则是Home键在左侧。
一般情形
所有界面都支持横竖屏切换
如果App的所有切面都要支持横竖屏的切换,那只需要勾选【General】 中的【Device Orientation】,选择希望支持的方向即可。
如上设置完之后,当设备竖屏的时候,所有的界面都是竖屏显示的;而当设备横屏Home在右侧时,所有的界面会横屏显示。其他方向不支持,界面不会改变。
这里有个坑:
在iOS 9 之后横屏时,状态栏会消失。
解决方法:确保plist 中的【View controller-based status bar appearance】为YES,然后重写ViewController的- (BOOL)prefersStatusBarHidden
,返回值是NO。- (BOOL)prefersStatusBarHidden {return NO; }
特殊情形
个别界面固定方向,其他所有界面都支持横竖屏切换
这种情况,在【General】-->【Device Orientation】中设置好支持的方向后,只需要在这些特殊的视图控制器中重写两个方法:
// 支持设备自动旋转 - (BOOL)shouldAutorotate {return YES; }/** * 设置特殊的界面支持的方向,这里特殊界面只支持Home在右侧的情况 */ - (UIInterfaceOrientationMask)supportedInterfaceOrientations {return UIInterfaceOrientationMaskLandscapeRight; }
个别界面支持横竖屏切换,其他所有界面都固定方向
可能大多数App会是这种需求,某些特殊界面只能横屏,如视频播放类App。
这里有两种处理方式:
方式一
在【General】-->【Device Orientation】中设置好需要支持的所有方向。然后使用一个基类控制器,在基类控制器中重写两个控制横竖屏的方法:
// 支持设备自动旋转 - (BOOL)shouldAutorotate {return YES; }// 支持竖屏显示 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {return UIInterfaceOrientationMaskPortrait; }
再然后,特殊的界面上再重写这俩方法,让其可以自动切换方向。
// 如果需要横屏的时候,一定要重写这个方法并返回NO - (BOOL)prefersStatusBarHidden {return NO; }// 支持设备自动旋转 - (BOOL)shouldAutorotate {return YES; }// 支持横屏显示 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {// 如果该界面需要支持横竖屏切换return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;// 如果该界面仅支持横屏// return UIInterfaceOrientationMaskLandscapeRight; }
方式二
用方式一的方法,还需要借助一个基类,所有的控制器都要继承这个基类,太麻烦?
另一种方式,是借助通知来控制界面的横竖屏切换。
还是整个App中大部分界面都是竖屏,某个界面可以横竖屏切换的情况。
首先,在【General】-->【Device Orientation】设置仅支持竖屏,like this:
然后在特殊的视图控制器里的ViewDidLoad中注册通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
通知方法的实现过程:
- (void)deviceOrientationDidChange {NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];[self orientationChange:NO];//注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight} else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];[self orientationChange:YES];} }- (void)orientationChange:(BOOL)landscapeRight {if (landscapeRight) {[UIView animateWithDuration:0.2f animations:^{self.view.transform = CGAffineTransformMakeRotation(M_PI_2);self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);}];} else {[UIView animateWithDuration:0.2f animations:^{self.view.transform = CGAffineTransformMakeRotation(0);self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);}];} } // 用到的两个宏:#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
最重要的一点:
需要重写如下方法,并且返回NO。- (BOOL)shouldAutorotate {return NO; }这样,在设备出于横屏时,界面就会变成横屏,设备处于竖屏时,界面就会变成竖屏。
填坑
上面方式二,因为【General】-->【Device Orientation】因为只设置了竖屏,所以当横屏时,如果有键盘弹出,键盘是竖屏时的样式。
解决办法:在【General】-->【Device Orientation】中加上横屏时的方向。如果VieController 是放在UINavigationController或者UITabBarController中,需要重写它们的方向控制方法。
// UINavigationController: - (BOOL)shouldAutorotate {return [self.topViewController shouldAutorotate]; }- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return [self.topViewController supportedInterfaceOrientations]; }// UITabBarController: - (BOOL)shouldAutorotate {return [self.selectedViewController shouldAutorotate]; }- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return [self.selectedViewController supportedInterfaceOrientations]; }
如果想要点击某个按钮之后,强制将竖屏显示的界面变成横屏呢?
有人可能会想到这样写:
// 横屏 - (IBAction)landscapAction:(id)sender {[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];[self orientationChange:YES]; }
但是按照上面的写法,会导致返回到之前的界面时,视图方向错误,即使返回前执行如下代码:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; [self orientationChange:NO];
也没有作用,下面是在开源工程中无意看到的写法:
// 横屏 - (IBAction)landscapAction:(id)sender {[self interfaceOrientation:UIInterfaceOrientationLandscapeRight]; }// 竖屏 - (IBAction)portraitAction:(id)sender {[self interfaceOrientation:UIInterfaceOrientationPortrait]; }- (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;[invocation setArgument:&val atIndex:2];[invocation invoke];} }
上面的方法会将设备的方向强制设置为某个方向,然后再监控设备方向改变的通知,即可实现横竖屏切换。
这里有一个用JS 和原生item 控制横竖屏切换的Demo。地址
这是效果图:
横竖屏切换总结就到这来了,Have Fun!
iOS 中横竖屏切换相关推荐
- iOS终极横竖屏切换解决方案
大家的项目都是只支持竖屏的吧?大多数朋友(这其中当然也包括博主),都没有做过横屏开发,这次项目刚好有这个需求,因此把横竖屏相关的心得写成一遍文章供诸位参考. 01.综述 大多数公司的项目都只支持竖屏, ...
- android中的横竖屏切换,Android中横竖屏切换时Activity的生命周期
Android中横竖屏切换时Activity的生命周期执行情况 1.默认情况下生命周期 (1)第一次进入界面 11-17 13:55:18.452: E/ImageListActivity(10586 ...
- Android开发中横竖屏切换的问题以及系统提供的常用Activity
Android开发中横竖屏切换的问题以及系统提供的常用Activity(总结) 2018年06月28日 16:18:45 北极熊的微笑 阅读数:72 横竖屏切换与状态保存的问题 前面也也说到了App横 ...
- [贝聊科技] iOS 终极横竖屏切换解决方案
大家的项目都是只支持竖屏的吧?大多数朋友(这其中当然也包括博主),都没有做过横屏开发,这次项目刚好有这个需求,因此把横竖屏相关的心得写成一遍文章供诸位参考. 01.综述 大多数公司的项目都只支持竖屏, ...
- iOS的横竖屏切换旋转(禁自动旋转)
这次做了视频的播放器,坑啊 ,好多,这不,刚刚爬上来,就来帮后来者填坑... 首先先说下横竖屏切换旋转的坑吧,,, 1. 在AppDelegate.h文件中 声明一个变量, @property (no ...
- iOS 6横竖屏切换
iOS6.0版本之前,UIViewController之间的横竖屏切换,只需设置一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInte ...
- iOS视频播放横竖屏切换技巧
一.需求:横竖屏切换. 二.效果: 三.实现: 如上图,点击工具栏的第四个按钮进行横屏切换: - (void)toolTabButtonPressed:(A ...
- iOS应用横竖屏切换
一.概述: 在iOS应用中,由UIViewController来控制屏幕翻转,根据需要随设备方向自动切换.在iOS6和之前的系统之间,控制方法发生了些变化. 二.视图伸缩属性: 1.UIVie ...
- ios 旋转屏幕试图切换_iOS屏幕横竖屏切换
iOS屏幕横竖屏切换 胡东东博客 • 2020 年 10 月 25 日 搜了网上的教程是真的乱,废话不多说,这里从启动到具体的VC,横竖屏切换完美搞定. 如果你的app只需要支持一个方向,那么不需要看 ...
最新文章
- 脉脉上发匿名消息,拼多多员工被开除了!
- 网络游戏的客户端同步问题
- 计算机学硕缩招,专硕扩招、学硕缩招!又有院校初试科目改了!本周这些院校发布最新消息!...
- android robust加固,Robust spectroscopic optical probe
- 关于pict工具进行测试用例的自动生成过程中:使用 pict.exe test.txt >test.xsl 导出为xls格式的表格文件时,出现拒绝访问的提示解决方案
- 创业基础(第六章:创业资源及其管理) 来自高校:全国大学生创新创业实践联盟 分类:创新创业 学习规则:按序学习
- 使用键盘操作将桌面计算机图标隐藏,如何创建键盘快捷方式来显示或隐藏桌面图标 | MOS86...
- 没有 本地计算机策略组,win10家庭版没有本地组策略编辑器怎么办
- System Silencer – 离开时 让电脑自动开始任务
- 听说有人谋求稳定的工作?
- c语言编写数据存储的游戏,c语言经典小程序和c语言编写的小游戏带注释(自动保存的).doc...
- /proc 文件系统中的文件及内容介绍
- python生成等值线_在python中生成X,Y数据的等值线图
- php支持postgresql,php支持postgresql
- 华为中央软件院编译器与编程语言实验室人才招募
- 关于Github上zheng项目部署问题总结
- 【无人机】基于fmincon实现无人机二维路径规划附matlab代码
- linux下网速测试
- ISO27001-2013学习笔记
- 100集华为HCIE安全培训视频教材整理 | IPSG
热门文章
- DNS原理、进阶、编译
- jQuery EasyUI 1.9.4中文参考手册 离线chm格式
- Camtasia Studio2023喀秋莎新增功能及电脑配置要求介绍
- 安卓adb shell模式下创建文件夹出现Permission denied时的解决办法
- gta5计算机或网络,《侠盗猎车手5(GTA5)》PC版需要连接网际网络及停止运行解决方法...
- php oss 断点续传,断点续传上传
- CNN FPGA加速器实现(小型)CNN FPGA加速器实现(小型)
- MATLAB取3维数组中的一层?
- CANoe/CANalyzer中常用CAPL诊断API分类总结与TFS应用示例
- 椭圆标准方程生成算法