目录:
layoutSubviews
Constrain to margins
Constraints
safeAreaLayoutGuide
Masonry
SnapKit
1. layoutSubviews
如果我们在viewDidLoad
里加载一个view, 可能最终呈现的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)viewDidLayoutSu