在剛開始學習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);
}];
思維導圖:
最終實現(xiàn)效果如下:
僅為復習以及筆記使用!