目錄:
- layoutSubviews
- Constrain to margins
- Constraints
- safeAreaLayoutGuide
- Masonry
- SnapKit
1. layoutSubviews
如果我們在viewDidLoad
里加載一個view, 可能最終呈現(xiàn)的frame與我們所設置的不一致. 又或者我們旋轉(zhuǎn)了屏幕, 界面沒有被適配. 這些情況下, 我們就需要在layoutSubviews
中重新指明frame布局.
為了驗證調(diào)用順序, 我們將重寫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
中確定. 其中layoutSubviews
是UIView
的方法, viewDidLayoutSubviews
是UIViewController
的方法.
如果我們使用代碼
或者xib
來加載一個view, 那么最好在viewDidLayoutSubviews
中重新設置一下frame, 而如果是storyboard加載的view, 則無需重新設置, 前提是設置了約束.
2. Constrain to margins
在使用storyboard布局的時候, 經(jīng)常會看到Constrain to margins
這個選項:
它的作用是在子view上添加一個邊界限制, 使其布局相對于父view有一個邊距. 如圖:
如果我們?nèi)∠催x, 使之上下左右約束為0, 則會鋪滿父視圖.
3. Constraints
Constraints約束決定了視圖的frame布局, 然而我們可以在代碼中動態(tài)修改Constraints的constant.
例如:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.centerYConstraint.constant = 100; // Y 下調(diào)100
NSLog(@"%s", __func__);
}
注
@property (readonly) CGFloat multiplier; // 比例只讀, 不可調(diào)
@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];
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);
}];
}
如果是劉海屏, 會有如下效果:
于是我們開始適配:
- (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);
}];
}
}
效果:
添加圖片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);
}];
添加紅綠藍子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);
}];
重新添加約束, 使紅綠藍豎排且高度相等:
[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]);
}];
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