iOS開發(fā)之進階篇(12)—— 屏幕適配

iOS-Hero.png

目錄:

  1. layoutSubviews
  2. Constrain to margins
  3. Constraints
  4. safeAreaLayoutGuide
  5. Masonry
  6. SnapKit

1. layoutSubviews

如果我們在viewDidLoad里加載一個view, 可能最終呈現(xiàn)的frame與我們所設置的不一致. 又或者我們旋轉了屏幕, 界面沒有被適配. 這些情況下, 我們就需要在layoutSubviews中重新指明frame布局.
為了驗證調用順序, 我們將重寫viewController的self.view的layoutSubviews方法:

KKView.m

#import "KKView.h"

@implementation KKView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    NSLog(@"%s", __func__);
}

- (void)layoutSubviews {
    
    NSLog(@"%s", __func__);
}

@end

viewController.m

#import "ViewController.h"
#import "KKView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%s", __func__);
    
    KKView *aView = [[KKView alloc] initWithFrame:self.view.bounds];
    self.view = aView;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"%s", __func__);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSLog(@"%s", __func__);
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    NSLog(@"%s", __func__);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    NSLog(@"%s", __func__);
}

@end

log:

2020-08-19 09:43:27.303328+0800 KKLayoutDemo[4069:80574] -[ViewController viewDidLoad]
2020-08-19 09:43:27.315342+0800 KKLayoutDemo[4069:80574] -[ViewController viewWillAppear:]
2020-08-19 09:43:27.320344+0800 KKLayoutDemo[4069:80574] -[ViewController viewWillLayoutSubviews]
2020-08-19 09:43:27.320512+0800 KKLayoutDemo[4069:80574] -[KKView layoutSubviews]
2020-08-19 09:43:27.320634+0800 KKLayoutDemo[4069:80574] -[ViewController viewDidLayoutSubviews]
2020-08-19 09:43:27.321131+0800 KKLayoutDemo[4069:80574] -[KKView drawRect:]
2020-08-19 09:43:27.361760+0800 KKLayoutDemo[4069:80574] -[ViewController viewDidAppear:]

在log順序中, KKView的frame在layoutSubviews或者viewDidLayoutSubviews中確定. 其中layoutSubviewsUIView的方法, viewDidLayoutSubviewsUIViewController的方法.

如果我們使用代碼或者xib來加載一個view, 那么最好在viewDidLayoutSubviews中重新設置一下frame, 而如果是storyboard加載的view, 則無需重新設置, 前提是設置了約束.

2. Constrain to margins

在使用storyboard布局的時候, 經(jīng)常會看到Constrain to margins這個選項:

Constrain to margins = YES

它的作用是在子view上添加一個邊界限制, 使其布局相對于父view有一個邊距. 如圖:

Constrain to margins = YES Effect

如果我們?nèi)∠催x, 使之上下左右約束為0, 則會鋪滿父視圖.

Constrain to margins = NO.png
Constrain to margins = NO Effect.png

3. Constraints

Constraints約束決定了視圖的frame布局, 然而我們可以在代碼中動態(tài)修改Constraints的constant.
例如:

NSLayoutConstraint.png
centerYConstraint.png
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.centerYConstraint.constant = 100;  // Y 下調100
    
    NSLog(@"%s", __func__);
}


@property (readonly) CGFloat multiplier; // 比例只讀, 不可調
@property CGFloat constant;

4. safeAreaLayoutGuide

iOS11之后UIView引入的一個新屬性

@property(nonatomic,readonly,strong) UILayoutGuide *safeAreaLayoutGuide API_AVAILABLE(ios(11.0),tvos(11.0));

??

    CGRect rect;
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (@available(iOS 11.0, *)) {
        rect = appDelegate.window.safeAreaLayoutGuide.layoutFrame;
    } else {
        rect = self.view.bounds;
    }
    
    KKView *aView = [[KKView alloc] initWithFrame:rect];
    [self.view addSubview:aView];
iPhone 8 & iPhone 11.png

5. Masonry

GitHub: https://github.com/SnapKit/Masonry

添加一個UIImageView, 設置邊界:

#import "Masonry.h"

- (void)viewDidLoad {
    [super viewDidLoad];
       
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Catalina"]];
    [self.view addSubview:imageView];
    
    UIEdgeInsets padding = UIEdgeInsetsMake(20, 20, 20, 20);
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(padding);
    }];
}

如果是劉海屏, 會有如下效果:

1.png

于是我們開始適配:

- (void)viewDidLoad {
    [super viewDidLoad];
        
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Catalina"]];
    [self.view addSubview:imageView];
    
    UIEdgeInsets padding = UIEdgeInsetsMake(20, 20, 20, 20);
    
//    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view).with.insets(padding);
//    }];
    
    if (@available(iOS 11.0, *)) {

        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).with.offset(padding.top);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft).with.offset(padding.left);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).with.offset(-padding.bottom);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight).with.offset(-padding.right);
        }];

    } else {

        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_top).with.offset(padding.top);
            make.left.equalTo(self.view.mas_left).with.offset(padding.left);
            make.bottom.equalTo(self.view.mas_bottom).with.offset(-padding.bottom);
            make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
        }];

    }
}

效果:

2.png

添加圖片title:

    UILabel *label = [UILabel new];
    label.text = @"Catalina";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor grayColor];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView);
        make.right.equalTo(imageView);
        make.top.equalTo(imageView);
        make.height.equalTo(@50);
    }];
3.png

添加紅綠藍子view:

    UIView *redView = [UIView new];
    redView.backgroundColor = [UIColor redColor];
    [imageView addSubview:redView];
    
    UIView *greenView = [UIView new];
    greenView.backgroundColor = [UIColor greenColor];
    [imageView addSubview:greenView];
    
    UIView *blueView = [UIView new];
    blueView.backgroundColor = [UIColor blueColor];
    [imageView addSubview:blueView];
    
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom);
        make.bottom.equalTo(imageView);
        make.left.equalTo(imageView);
        make.width.equalTo(imageView).multipliedBy(1.0/3.0);
    }];
    
    [greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.and.width.equalTo(redView);
        make.left.equalTo(redView.mas_right);
    }];
    
    
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.and.width.equalTo(greenView);
        make.left.equalTo(greenView.mas_right);
    }];
4.png

重新添加約束, 使紅綠藍豎排且高度相等:

    [redView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom);
        make.left.right.equalTo(imageView);
        make.bottom.equalTo(greenView.mas_top);
    }];

    [greenView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(imageView);
        make.bottom.equalTo(blueView.mas_top);
    }];
    
    [blueView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.equalTo(imageView);
        make.height.equalTo(@[redView, greenView]);
    }];
5.png

PS
mas_makeConstraints() 添加約束
mas_remakeConstraints() 移除之前的約束,重新添加新的約束
mas_updateConstraints() 更新約束,寫哪條更新哪條,其他約束不變

equalTo() 參數(shù)是對象類型,一般是視圖對象或者mas_width這樣的坐標系對象
mas_equalTo() 和上面功能相同,參數(shù)可以傳遞基礎數(shù)據(jù)類型對象,可以理解為比上面的API更強大

width() 用來表示寬度,例如代表view的寬度
mas_width() 用來獲取寬度的值。和上面的區(qū)別在于,一個代表某個坐標系對象,一個用來獲取坐標系對象的值

6. SnapKit

Masonry 的Swift 版本.
GitHub: https://github.com/SnapKit/SnapKit

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

友情鏈接更多精彩內(nèi)容