Autorotate - 让你的应用支持旋转

iOS App大多数情况下都是只支持竖屏的,少部分页面才支持旋转,甚至有些页面需要强制横屏。本文将介绍应用如何支持旋转,以及旋转后我们页面需要如何处理的。

UIDeviceOrientation和UIInterfaceOrientation

iOS旋转包括设备旋转和界面旋转,设备方向我们控制不了,看用户怎么握住他的手机,界面方向一般跟着设备方向旋转而旋转(如果支持的话),通常我们说的APP支持旋转,就是界面旋转。

屏幕快照 2018-07-02 上午9.56.41.png

UIDeviceOrientation:设备方向

iOS设备方向是通过加速计来获取的,总共有七个方向:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
} __TVOS_PROHIBITED;

当设备方向变化时候,会发出通知UIDeviceOrientationDidChangeNotification,注册监听该通知来处理页面。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //开启和监听 设备旋转的通知(不开启的话,设备方向一直是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];
}

UIInterfaceOrientation:界面方向

iOS界面方向和Home的按钮方向是一致的,总共有五个方向:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;

当界面方向变化时候,会发出通知UIApplicationWillChangeStatusBarOrientationNotificationUIApplicationDidChangeStatusBarOrientationNotification,注册监听该通知来处理页面。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[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;
    }
}

//最后在dealloc中移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

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

支持旋转(不建议):

在项目的General-->Deployment Info-->Device Orientation中,勾选支持的方向,一般Upside Down不勾选,没几个APP会支持颠倒。

屏幕快照 2018-07-02 上午9.43.32.png

备注:为什么不建议呢?因为这样设置的话,如果你的启动页使用的是LaunchScreen.storyboard,那么当你横屏启动APP时,启动页也会横屏启动,而不是竖屏启动,这很怪异(一般app启动页面都是竖屏的);然而,貌似使用LaunchImage设置启动页,有不受影响,当你横屏启动APP时,启动页依然是竖屏,有点没搞懂。

在AppDelegate中实现代理:(建议)

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}

备注:注意,如果APP有多个window,判断下window回更合理些,这里返回的是keyWindow;当然,你所有window都支持该旋转方向,也不是不可以。

支持ViewController屏幕方向设置

我们需要通过重载ViewController方法,来控制界面是否支持旋转,以及旋转方向。

WDMainTabBarController

#pragma mark - Orientation

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

WDBaseNavigationController

#pragma mark - Orientation

- (BOOL)shouldAutorotate {
    return [[self.viewControllers lastObject] shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

WDBaseViewController

由于APP默认仅支持竖屏,所有我写了个基类BaseViewController,在基类中实现如下方法:

#pragma mark - Orientation

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

WDWebViewController

WDWebViewController支持制动旋转

#pragma mark - Orientation

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

WDThirdViewController

WDThirdViewController强制横屏

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"Third";
    
    self.oldOrientation = [UIDevice currentDevice].orientation;
    if (self.oldOrientation == UIDeviceOrientationLandscapeLeft) {
        // 如果设备本身是横屏的,但界面不是横屏,这时候直接设置LandscapeLeft不生效,需要设置Unknown欺骗系统后,再设置横屏
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (interfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight: {
            // 本身是横屏,直接break
            break;
        }
            
        default: {
            // 强制横屏
            [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
            break;
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // 还原设备方向
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:self.oldOrientation] forKey:@"orientation"];
}

#pragma mark - Orientation

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

至于旋转页面的处理,我在WDBaseViewController中添加了通知和方法:

//监听UIApplicationDidChangeStatusBarOrientationNotification的处理
- (void)handleStatusBarOrientationChange: (NSNotification *)notification {
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    BOOL isLandscape = NO;
    switch (interfaceOrientation) {
        case UIInterfaceOrientationUnknown: {
            //NSLog(@"未知方向");
            break;
        }
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown: {
            isLandscape = NO;
            break;
        }
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight: {
            isLandscape = YES;
            break;
        }
            
        default:
            break;
    }
    
    CGFloat navBarHeight = WD_NAV_BAR_HEIGHT;
    if (isLandscape) {
        navBarHeight = WD_NAV_BAR_HEIGHT_LANDSCAPE;
    }
    
    [self updateLayoutNavBarWhenAutorotate:navBarHeight isLandscape:isLandscape];
}

- (void)updateLayoutNavBarWhenAutorotate:(CGFloat)navBarHeight isLandscape:(BOOL)isLandscape {
    self.navigationBarBackImageView.frame = CGRectMake(0, 0, WD_SCREEN_WIDTH, navBarHeight);
    self.navigationBarLineView.frame = CGRectMake(0, navBarHeight - 0.5, WD_SCREEN_WIDTH, 0.5);
}

子类WDBaseWebViewController重载

- (void)updateLayoutNavBarWhenAutorotate:(CGFloat)navBarHeight isLandscape:(BOOL)isLandscape {
    [super updateLayoutNavBarWhenAutorotate:navBarHeight isLandscape:isLandscape];
    
    CGRect frame =CGRectMake(0, navBarHeight, WD_SCREEN_WIDTH, WD_SCREEN_HEIGHT - navBarHeight);
    self.webView.frame = frame;
}

最后

这里有几个地方需要注意的,竖屏和横屏之后的navigationBar和tabBar的宽高都发生了变化,而且不同设备之间大小有差异,比如:

  • iphone 7竖屏时,navigationBar的宽高是(375, 44),statusBar的宽高是(375, 20),横屏时,navigationBar宽高是(667, 32),statusBar的宽高是(667, 0)
  • iphone x竖屏时,navigationBar的宽高是(375, 44),statusBar的宽高是(375, 44),横屏时,navigationBar宽高是(812, 32),statusBar的宽高是(812, 0)

还有一个有意思的是:当你横屏的时候,状态栏会被隐藏不见了(可能是横屏为了更多的展示内容),如果我们需要将状态栏显示出来,ViewController需要重载prefersStatusBarHidden方法,因此我在WDBaseViewController中添加如下方法:

#pragma mark - Status Bar

- (BOOL)prefersStatusBarHidden {
    return NO;
}

在WDBaseNavigationController中添加如下方法:

#pragma mark - Status Bar

- (UIViewController *)childViewControllerForStatusBarHidden {
    return self.topViewController;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

but 在iphone x中,无论我如何尝试,状态栏都不会出现,始终隐藏,不得其解。

备注:通过CGRectGetWidth([UIScreen mainScreen].bounds),获取的宽高都是旋转后的值。

Demo地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容