实用小技巧(二):屏幕横竖屏的判断和相关逻辑

版本记录

版本号 时间
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阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249

推荐阅读更多精彩内容