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

横屏两种情况是反的你知道吗?

UIInterfaceOrientationLandscapeRightUIInterfaceOrientationMaskLandscapeRight都代表横屏,Home键在右侧的情况;UIDeviceOrientationLandscapeLeft则是Home键在左侧。

一般情形

所有界面都支持横竖屏切换
如果App的所有切面都要支持横竖屏的切换,那只需要勾选【General】 中的【Device Orientation】,选择希望支持的方向即可。

图中支持竖屏和Home在右侧

如上设置完之后,当设备竖屏的时候,所有的界面都是竖屏显示的;而当设备横屏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:

Device Orientation

然后在特殊的视图控制器里的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。地址
这是效果图:

横竖屏切换.gif

iOS 中各种横竖屏切换总结的更多相关文章

  1. iOS播放器横竖屏切换

    http://www.cocoachina.com/cms/wap.php?action=article&id=20292 http://feihu.me/blog/2015/how-to-h ...

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

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

  3. android activity在横竖屏切换的时候不重新调用onCreate方法

    在安卓系统中,横竖屏切换会默认重新调用onCreate等生命周期方法,如果此时有一些临时数据没有保存下来,很有可能会导致该数据丢失. 因此我们可以进行以下设置,来避免恒切换时重新调用onCreate方 ...

  4. Activity嵌套多个Fragment实现横竖屏切换

    一.上图 二.需求 最近项目遇到个横竖屏切换的问题.较为复杂.在此记之. 1.Activity中竖屏嵌套3个Fragment,本文简称竖屏FP1,FP2,FP3. 2.当中竖屏FP1与FP2能够切换为 ...

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

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

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

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

  7. Android 中Activity生命周期分析:Android中横竖屏切换时的生命周期过程

    最近在面试Android,今天出了一个这样的题目,即如题: 我当时以为生命周期是这样的: onCreate --> onStart -- ---> onResume ---> onP ...

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

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

  9. 在Android中自动实现横竖屏切换的问题

    http://developer.android.com/training/basics/supporting-devices/screens.html参照Google推荐的做法 在你项目的res 文 ...

随机推荐

  1. Docs-.NET-C#-指南-语言参考-关键字-值类型:struct

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-值类型:struct 1.返回顶部 1. struct(C# 参考) 2015/07/20 struct 类型是一种值类型,通常用来封 ...

  2. Qt编写自定义控件61-通用移动

    一.前言 通用移动类,目标就是为了实现放入任意的控件以后,支持鼠标拖动,在容器中或者父类中拖动,这个应用场景非常多,比如在地图上放置的设备,需要用户自行按下拖动到指定的合适的位置,然后保存设备的位置坐 ...

  3. oracle 索引聚簇表的工作原理

    作者:Richard-Lui 一:首先介绍一下索引聚簇表的工作原理:(先创建簇,再在簇里创建索引,创建表时指定列的簇类型) 聚簇是指:如果一组表有一些共同的列,则将这样一组表存储在相同的数据库块中:聚 ...

  4. Linux系统调优——Memory内存(二)

    (1).查看Memory(内存)运行状态相关工具 1)free命令查看内存使用情况 [root@youxi1 ~]# free -m //-m选项,以MB为单位显示 total used free s ...

  5. Spring AOP无法拦截Controller

    参考@参考文章中的评论 首先,应该打开aop代理 <aop:aspectj-autoproxy proxy-target-class="true"/> 其次,应该讲ao ...

  6. this page isn't working (ERR_EMPTY_RESPONSE)

    特定情况触发了PHP的Call to undefined function(函数不存在)的Fatal error(致命错误),PHP异常终止执行,Apache收到PHP的异常信号时,认为PHP处理请求 ...

  7. JS正则验证数字格式2

    之前的博文:JS验证正数字,正则的一种正数规则1,中isNaN可以判断内容是否为数字,但是这种判断出来的数字,有的不是数字的标准格式.那篇博文中尝试了下用正则验证,但是忘了一种情况,小数点后无数字,小 ...

  8. HTML布局排版1清除body的margin

    观察可发现,一般的HTML页面分为上中下三部分,上边是导航一栏,中间是内容,下方是页面的下部分.注意html里body本身自带8px的上下左右外边距,如图,在qq浏览器和ie里可以看到body本身是8 ...

  9. 【Leetcode_easy】970. Powerful Integers

    problem 970. Powerful Integers solution: class Solution { public: vector<int> powerfulIntegers ...

  10. package ‘RPMM’ is not available (for R version 3.6.0)

    当我们用启动R安装一些R包的时候 提示: Warning: unable to access index for repository https://mirrors.eliteu.cn/CRAN/s ...