屏幕橫豎屏轉換適配的應用

版本記錄

版本號 時間
V1.0 2017.08.04

前言

??很多時候我們的app都需要橫豎屏的適配,并且根據(jù)手機的方向自動的更改屏幕的方向,下面我們就說一下屏幕橫豎屏適配的方法。前面我也寫過類似的一篇文章,但是有人說寫的有點啰嗦,所以我又寫了一個簡化的demo,希望對大家有所幫助,下面先給出上一篇文章相關鏈接。
1. 實用小技巧(二):屏幕橫豎屏的判斷和相關邏輯

適配前提和基礎

在我們進行橫豎配之前我們需要幾個前提和基礎。

1. 交互方向

交互方向是一個枚舉值,UIInterfaceOrientation,大家可以看一下

//狀態(tài)欄方向,可以看見橫屏時和設備方向是反的
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;

2. 設備方向

這個和上面那個方向不是一回事,這個也是一個枚舉UIDeviceOrientation,下面我們還是看一下

//設備方向,具體可以參考home鍵的位置,這里給的已經(jīng)很清楚了
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;

3. 設備橫屏后狀態(tài)欄的顯示

默認情況下,橫屏以后是不會顯示狀態(tài)欄的,所以我們要采取下面的措施。

第一步:設置info.plist

如下圖所示。

info.plist配置

第二步:代碼階段

在要橫屏顯示的控制器里面viewDidLoad里面加上如下代碼。

    //橫屏顯示狀態(tài)欄 ios9以后已經(jīng)棄用,后面會有替代方法介紹
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

適配代碼實現(xiàn)

下面看一下適配的代碼。

1. JJScreenOritentionVC.m
#import "JJScreenOritentionVC.h"
#import "Masonry.h"

#define kScreenWidth    [UIScreen mainScreen].bounds.size.width
#define kScreenHeight   [UIScreen mainScreen].bounds.size.height

@interface JJScreenOritentionVC ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation JJScreenOritentionVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupUI];
    
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ||
        [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self layoutPortraitView];
    }
    else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ||
             [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight){
        [self layoutLandscapeView];
    }
    
    //監(jiān)聽屏幕的旋轉方向
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOritentionDidChanged) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    //橫屏顯示狀態(tài)欄 ios9以后已經(jīng)棄用,后面會有替代方法介紹
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.view.backgroundColor = [UIColor yellowColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"這個可怎么破?";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.8 alpha:1.0];
    [self.view addSubview:label];
    self.label = label;
}

//豎屏方向

- (void)layoutPortraitView
{
    [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.equalTo(@(kScreenWidth - 20));
        make.height.equalTo(@30);
    }];
}

//橫屏方向

- (void)layoutLandscapeView
{
    //注意:這里一定要用remake更新原先的約束,要不是變不過來的
    [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.equalTo(@(kScreenWidth - 20));
        make.height.equalTo(@30);
    }];
}

#pragma mark - Action && Notification

- (void)deviceOritentionDidChanged
{
    UIDeviceOrientation deviceOrient = [UIDevice currentDevice].orientation;
    
    NSLog(@"%ld",deviceOrient);
    
    if (deviceOrient == UIDeviceOrientationPortrait || deviceOrient == UIDeviceOrientationPortraitUpsideDown) {
        NSLog(@"豎屏");
        
        //設置豎屏視圖
        [self layoutPortraitView];
    }
    else if(deviceOrient == UIDeviceOrientationLandscapeLeft){
        
        NSLog(@"橫屏");
        
        [self layoutLandscapeView];
    }
    else if(deviceOrient == UIDeviceOrientationLandscapeRight){
        
        NSLog(@"橫屏");
        
        [self layoutLandscapeView];
    }
}

@end

具體代碼就這么多,很簡單吧。


適配效果驗證

下面就看一下適配效果驗證。

豎屏
橫屏

當你改變手機的方向的時候,就會觸發(fā)通知,自使用的更改方向了。

后記

關于屏幕適配,這里是第二篇了,希望能解決大家的基本困惑,關于更深層次這方面的應用,我會后續(xù)繼續(xù)和大家分享,希望對大家有所幫助。

這個和我家還是很像的
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 關于橫豎屏適配,有一句說一句,坑挺深的。之前做Vision和畢設的時候就處理過橫豎屏問題,不過當時的功力太淺,明顯...
    HarwordLiu閱讀 37,604評論 26 137
  • 原文地址在我的個人主頁 你可能非常了解用不同的方式去適配不同尺寸的iPhone屏幕,在適配iPhone屏幕時你需要...
    jmstack閱讀 7,587評論 1 22
  • 目錄 一、最讓人糾結的三種枚舉 二、兩種屏幕旋轉的觸發(fā)方式 三、屏幕旋轉控制的優(yōu)先級 四、開啟屏幕旋轉的全局權限 ...
    來鬧的閱讀 3,100評論 0 4
  • 1. 橫豎屏方向枚舉 關于橫豎屏一共有三種枚舉,UIInterfaceOrientation,UIInterfac...
    沉江小魚閱讀 1,058評論 0 2
  • 一、監(jiān)聽橫豎屏的切換 1、通知方式: 如果使用這個通知,當iPhone/iPad旋轉的時候,你會得到的旋轉方向會是...
    CholMay閱讀 12,954評論 6 33

友情鏈接更多精彩內容