實用小技巧(二):屏幕橫豎屏的判斷和相關邏輯

版本記錄

版本號 時間
V1.0 2017.06.02

前言

一般我們用的app都是豎屏的,比如微信和QQ等,但是也有一些app是橫屏的,比如有些游戲,還有一些是橫豎屏都可以的,比如視頻直播類,如花椒、快手等,所以這里就先講一下橫豎屏適配的相關技巧。感興趣的可以看看我寫的其他小技巧
1. 實用小技巧(一):UIScrollView中上下左右滾動方向的判斷

詳情

一、基本配置

要是工程可以適應橫豎屏,那么工程得按照下面進行配置。

橫豎屏工程配置

還有就是真機的豎屏鎖定不能開著,也就是說不能鎖定豎屏。

豎屏鎖定

二、代碼實現

下面就直接看代碼吧,我寫的這個demo,實現的就是橫豎屏轉換和內部視圖的適配。

1. AppDelegate.m

#import "AppDelegate.h"
#import "JJLiveVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    JJLiveVC *liveVC = [[JJLiveVC alloc] init];
    self.window.rootViewController = liveVC;
    [self.window makeKeyAndVisible];
    
    //橫屏顯示狀態欄
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    
    return YES;
}

@end
2. JJLiveVC.h

#import <UIKit/UIKit.h>

@interface JJLiveVC : UIViewController

@end
3. JJLiveVC.m

#import "JJLiveVC.h"
#import "JJLiveView.h"

@interface JJLiveVC ()

@property (nonatomic, strong) JJLiveView *liveView;

@end

@implementation JJLiveVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientDidChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
        
        //豎屏
        [self setupLivePortraitView];
    }
    else {
        
        //橫屏
        [self setupLiveLandscapeView];
    }
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

#pragma mark - Object Private Function

//設置豎屏視圖
- (void)setupLivePortraitView
{
    if (self.liveView) {
        self.liveView.frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.liveView.isLandscape = NO;
        return;
    }
    JJLiveView *liveView = [[JJLiveView alloc] initWithFrame:self.view.frame];
    self.liveView = liveView;
    liveView.isLandscape = NO;
    [self.view addSubview:liveView];
}

//設置橫屏視圖
- (void)setupLiveLandscapeView
{
    if (self.liveView) {
        self.liveView.frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.liveView.isLandscape = YES;
        return;
    }
    
    JJLiveView *liveView = [[JJLiveView alloc] initWithFrame:self.view.frame];
    self.liveView = liveView;
    liveView.isLandscape = YES;
    [self.view addSubview:liveView];
}


#pragma mark - Action && Notification

- (void)orientDidChanged:(NSNotification *)noti
{
    UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
    
    if (orient == UIDeviceOrientationPortrait) {
        NSLog(@"豎屏");
        //設置豎屏視圖
        [self setupLivePortraitView];
        
    }
    else {
        NSLog(@"橫屏");
        
        if (orient == UIDeviceOrientationLandscapeRight || orient == UIDeviceOrientationLandscapeLeft){
            //設置橫屏視圖
            [self setupLiveLandscapeView];
        }
    }
}

@end

4. JJLiveView.h

#import <UIKit/UIKit.h>

@interface JJLiveView : UIView

@property (nonatomic, assign) BOOL isLandscape;

@end

5. JJLiveView.m

#import "JJLiveView.h"

@interface JJLiveView ()

@property (nonatomic, strong) UILabel *timeOnLabel;
@property (nonatomic, strong) UIButton *startLiveButton;

@end

@implementation JJLiveView

#pragma mark - Override Base Function

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.backgroundColor = [UIColor darkGrayColor];
    
    //節目時間已經到啦
    UILabel *timeOnLabel = [[UILabel alloc] init];
    timeOnLabel.textColor = [UIColor blueColor];
    timeOnLabel.text = @"小澤老師的直播時間到了~~~~~";
    timeOnLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [self addSubview:timeOnLabel];
    self.timeOnLabel = timeOnLabel;
    
    //開始直播按鈕
    UIButton *startLiveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [startLiveButton setTitle:@"開始直播啦" forState:UIControlStateNormal];
    startLiveButton.layer.cornerRadius = 21.5;
    startLiveButton.layer.masksToBounds = YES;
    startLiveButton.layer.borderColor = [UIColor redColor].CGColor;
    startLiveButton.layer.borderWidth = 0.5;
    [startLiveButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    startLiveButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:21.5];
    [startLiveButton addTarget:self action:@selector(startLiveButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:startLiveButton];
    self.startLiveButton = startLiveButton;
}

//進行豎屏約束
- (void)layoutPortraitView
{
    //文字
    [self.timeOnLabel sizeToFit];
    [self.timeOnLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    //按鈕
    [self.startLiveButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-100.0);
        make.height.equalTo(@40);
        make.width.equalTo(@160);

    }];
}

//進行橫屏約束
- (void)layoutLandscapeView
{
    //文字
    [self.timeOnLabel sizeToFit];
    [self.timeOnLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    //按鈕
    [self.startLiveButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-50.0);
        make.height.equalTo(@40);
        make.width.equalTo(@160);
    }];
}

#pragma mark - Getter && Setter

- (void)setIsLandscape:(BOOL)isLandscape
{
    _isLandscape = isLandscape;
    
    if (!isLandscape) {
        //進行豎屏約束
        [self layoutPortraitView];
    }
    else {
        //進行橫屏約束
        [self layoutLandscapeView];
    }
}

#pragma mark - Action && Notification

- (void)startLiveButtonDidClick:(UIButton *)button
{

}

@end
6. PrefixHeader.pch

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#import "Masonry.h"

#endif

下面看實現效果圖

portrait
landscape_left
landscape_right

在寫的過程中碰到了兩個小問題,現和大家分享:

  • 第一個是將狀態欄變成白色。
  • 橫屏的時候狀態欄不顯示了,消失了。

狀態欄修改為白色

其實這個很簡單,只需要兩步,第一步就是在info.plist中加一個鍵值對,View controller-based status bar appearance設置為NO。第二步就是在控制器內部加上修改顏色的代碼,[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];具體需要什么顏色需要看枚舉值,大家可以自己看,我需要的是白色的,所以我寫的是UIStatusBarStyleLightContent。

橫屏了狀態欄消失了

這個可以在appDelegate的啟動方法里面加入兩句代碼即可。

    //橫屏顯示狀態欄
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

運行起來就發現橫屏的時候狀態欄顯示出來了。

后記

更多的實用小技巧和大家分享哦,未完,待續~~~~

風景圖
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,363評論 6 532
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,497評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,305評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,962評論 1 311
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,727評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,193評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,257評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,411評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,945評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,777評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,978評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,519評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,216評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,642評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,878評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,657評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,960評論 2 373

推薦閱讀更多精彩內容