在刚开始学习UI的时候,关于布局我们都是从SB开始学习.一般都也遵循从左往右,从上往下.但是遇到scrollView的自动布局.我们就无从下手了.
由于代码比较简单,就不做详细解释.奉上代码和思维导图.
代码:
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.scrollView];
_containerView = [[UIView alloc] init];
[self.scrollView addSubview:_containerView];
__weak typeof(self) weakself = self;
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(weakself.view);
}];
[_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(weakself.scrollView);
make.size.mas_equalTo(CGSizeMake(weakself.scrollView.bounds.size.width, 1000));
}];
UIButton *btn = [[UIButton alloc] init];
[btn setTitle:@"测试" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_containerView addSubview:btn];
//将按钮添加到scrollView上,
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 40));
make.left.equalTo(_containerView).offset(0);
make.top.equalTo(_containerView).offset(100);
}];
思维导图:
最终实现效果如下:
仅为复习以及笔记使用!