iOS的横、竖屏和旋转

前言:

        iOS App中大多数页面是只展示竖屏下的效果,但是少部分页面需要支持横竖屏。本文分别介绍监听屏幕旋转方向、视图控制器中旋转方向的设置、屏幕旋转方向下的视图处理以及强制横屏的处理梳理。

一、监听屏幕旋转方向

在处理iOS横竖屏时,经常会和UIDeviceOrientation、UIInterfaceOrientation和UIInterfaceOrientationMask这三个枚举类型打交道,它们从不同角度描述了屏幕旋转方向。

1、UIDeviceOrientation:设备方向

iOS的设备方向是通过iOS的加速计来获取的。

1)iOS定义了以下七种设备方向

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,                 // 未知方向,可能是设备(屏幕)斜置
    UIDeviceOrientationPortrait,                // 设备(屏幕)直立
    UIDeviceOrientationPortraitUpsideDown,      // 设备(屏幕)直立,上下顛倒
    UIDeviceOrientationLandscapeLeft,           // 设备(屏幕)向左横置
    UIDeviceOrientationLandscapeRight,          // 设备(屏幕)向右橫置
    UIDeviceOrientationFaceUp,                  // 设备(屏幕)朝上平躺
    UIDeviceOrientationFaceDown                 // 设备(屏幕)朝下平躺
};

说明:UIDeviceOrientation参考home键方向,如:home方向在右,设备(屏幕)方向向左(UIDeviceOrientationLandscapeLeft)

2)读取设备方向

UIDevice单例代表当前的设备。从这个单例中可以获得的信息设备,如设备方向orientation。

          UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

3)监听、处理和移除 设备方向改变的通知

当设备方向变化时候,发出UIDeviceOrientationDidChangeNotification通知;注册监听该通知,可以针对不同的设备方向处理视图展示。

//开启和监听 设备旋转的通知(不开启的话,设备方向一直是UIInterfaceOrientationUnknown)
if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleDeviceOrientationChange:) 
                                     name:UIDeviceOrientationDidChangeNotification object:nil];
//设备方向改变的处理
- (void)handleDeviceOrientationChange:(NSNotification *)notification{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    switch (ddeviceOrientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕朝上平躺");
            break;
        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕朝下平躺");
            break;
        case UIDeviceOrientationUnknown:
            NSLog(@"未知方向");
            break;
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"屏幕向左横置");
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"屏幕向右橫置");
            break;
        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立,上下顛倒");
            break;
        default:
            NSLog(@"无法辨识");
            break;
    }
}
//最后在dealloc中移除通知 和结束设备旋转的通知
- (void)dealloc{
    //...
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}

说明:手机锁定竖屏后,UIDeviceOrientationDidChangeNotification通知就失效了。

2、UIInterfaceOrientation:界面方向

界面方向是反应iOS中界面的方向,它和Home按钮的方向是一致的。

1)iOS定义了以下五种界面方向

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,       //未知方向
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,               //界面直立
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,  //界面直立,上下颠倒
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,   //界面朝左
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft    //界面朝右
} __TVOS_PROHIBITED;

说明:从定义可知,界面方向和设别方向有对应关系,如界面的竖直方向就是 设备的竖直方向:
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown

2)读取界面方向

UIInterfaceOrientation和状态栏有关,通过UIApplication的单例调用statusBarOrientation来获取.

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

3)监听、处理和移除 界面方向改变的通知

当界面方向变化时候,先后发出UIApplicationWillChangeStatusBarOrientationNotificationUIApplicationDidChangeStatusBarOrientationNotification通知;注册监听这两个通知,可以针对不同的界面方向处理视图展示。

//以监听UIApplicationDidChangeStatusBarOrientationNotification通知为例
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleStatusBarOrientationChange:) 
                                     name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
//界面方向改变的处理
- (void)handleStatusBarOrientationChange: (NSNotification *)notification{
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (interfaceOrientation) {
        case UIInterfaceOrientationUnknown:
            NSLog(@"未知方向");
            break;
        case UIInterfaceOrientationPortrait:
            NSLog(@"界面直立");
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            NSLog(@"界面直立,上下颠倒");
            break;
        case UIInterfaceOrientationLandscapeLeft:
            NSLog(@"界面朝左");
            break;
        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"界面朝右");
            break;
        default:
            break;
    }
}

说明:手机锁定竖屏后,UIApplicationWillChangeStatusBarOrientationNotification和 UIApplicationDidChangeStatusBarOrientationNotification通知也失效了。

3、UIInterfaceOrientationMask

UIInterfaceOrientationMask是为了集成多种UIInterfaceOrientation而定义的类型,和ViewController相关,一共有7种

1)iOS中的UIInterfaceOrientationMask定义

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
} __TVOS_PROHIBITED;

2)UIInterfaceOrientationMask的使用

在ViewController可以重写- (UIInterfaceOrientationMask)supportedInterfaceOrientations方法返回类型,来决定UIViewController可以支持哪些界面方向。

    //支持界面直立
   - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
   return UIInterfaceOrientationMaskPortrait;
   }

总结:UIDeviceOrientation(设备方向)UIInterfaceOrientation(屏幕方向)是两个不同的概念。前者代表了设备的一种状态,而后者是屏幕为了应对不同的设备状态,做出的用户界面上的响应。在iOS设备旋转时,由UIKit接收到旋转事件,然后通过AppDelegate通知当前程序的UIWindow对象,UIWindow对象通知它的rootViewController,如果该rootViewController支持旋转后的屏幕方向,完成旋转,否则不旋转;弹出的ViewController也是如此处理。

二、视图控制器中旋转方向的设置

0、关于禁止横屏的操作(不建议)
比较常规的方法有两种:
方法1:在项目的General-->Deployment Info-->Device Orientation中,只勾选Portrait(竖屏)

勾选Portrait.png

方法2:Device Orientation默认设置,在Appdelegate中实现supportedInterfaceOrientationsForWindow:只返回UIInterfaceOrientationMaskPortraitt(竖屏)

-  (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  {  
 return UIInterfaceOrientationMaskPortrait;  

}
说明:极少的APP中所有界面都是竖屏的,因为总会有界面需要支持横屏,如视频播放页。所以不建议设置禁止APP页面横屏.

下面介绍如何让项目中的 视图控制器中旋转方向的设置

1、APP支持多个方向

APP支持多个方向.png

说明:如此,APP支持横屏和竖屏了,但是具体视图控制器支持的页面方向还需要进一步处理。由于不支持竖屏颠倒(Upside Down),即使设备上下颠倒,通过API也不会获得设备、屏幕上下颠倒方向的。

2、支持ViewController屏幕方向设置

1)关键函数

视图控制器支持的界面方向主要由以下三个函数控制

//是否自动旋转,返回YES可以自动旋转,返回NO禁止旋转 
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  
//返回支持的方向  
- (UIInterfaceOrientationMask)supportedInterfaceOrientations 
NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  
//由模态推出的视图控制器 优先支持的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

2) QSBaseViewController设置

//QSBaseViewController.h
@interface QSBaseController : UIViewController
@end
//QSBaseViewController.m
@implementation QSBaseController
  //#pragma mark - 控制屏幕旋转方法
//是否自动旋转,返回YES可以自动旋转,返回NO禁止旋转
- (BOOL)shouldAutorotate{
    return NO;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
//由模态推出的视图控制器 优先支持的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}
@end

说明1:QSBaseViewController默认不支持旋转,只支持界面竖直方向,项目中的Controller都继承自QSBaseViewController,可以通过重写这三个方法来让Controller支持除竖屏之外的方向或旋转。

3) 在QSNavigationController设置

目标:通过QSNavigationController来push视图控制器时,把支持屏幕旋转的设置交给最新push进来([self.viewControllers lastObject])的viewController来设置。

//QSNavigationController.h
@interface QSNavigationController : UINavigationController
@end
//QSNavigationController.m
@implementation QSNavigationController
#pragma mark - 控制屏幕旋转方法
- (BOOL)shouldAutorotate{  
    return [[self.viewControllers lastObject]shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [[self.viewControllers lastObject]supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

4) 在QSTabBarController设置

目标:TabBarController通常作为整个程序的rootViewController,UITabBar上面显示的每一个Tab都对应着一个ViewController;每点击一个Tab,出现的ViewController(self.selectedViewController)对屏幕旋转和支持方向的设置 交给其自身去控制。

//QSTabBarController.h
@interface QSTabBarController : UITabBarController
@end
//QSTabBarController.m
@implementation QSTabBarController
#pragma mark - 控制屏幕旋转方法
- (BOOL)shouldAutorotate{
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 229,460评论 6 538
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,067评论 3 423
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 177,467评论 0 382
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,468评论 1 316
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,184评论 6 410
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,582评论 1 325
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,616评论 3 444
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,794评论 0 289
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,343评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,096评论 3 356
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,291评论 1 371
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,863评论 5 362
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,513评论 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,941评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,190评论 1 291
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,026评论 3 396
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,253评论 2 375

推荐阅读更多精彩内容