Once
Articles25
Tags10
Categories0
Masonr布局对UIScrollView的内容自适应

Masonr布局对UIScrollView的内容自适应

Masonr布局对UIScrollView的内容自适应

1.添加UIscrollview到self.view上,并设置约束等于父视图,设置宽度。

self.scrollView.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(100);
    make.left.right.equalTo(self.view);
    make.bottom.equalTo(self.view);
}];

2.使用容器视图添加到UIscrollview上,所有的子视图布局在这个容器视图上。

[self.scrollView addSubview:self.contentView];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.scrollView);
    make.width.equalTo(self.scrollView);
}];
[self.contentView addSubview:self.labTitle02];
[self.labTitle01 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.contentView.mas_top).offset(10);
    make.width.mas_equalTo(kScreenWidth-20);
    make.centerX.equalTo(self.contentView);
}];
[self.labTitle02 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.labTitle01.mas_bottom).offset(10);
    make.width.mas_equalTo(kScreenWidth-20);
    make.centerX.equalTo(self.contentView);
}];

3.设置布局的最底部的视图,容器视图的bottom等于最后一个视图的bottom

[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.labTitle03.mas_bottom).offset(30);
}];
Author:Once
Link:http://once00.github.io/2023/10/12/Masonr%E5%B8%83%E5%B1%80%E5%AF%B9UIScrollView%E7%9A%84%E5%86%85%E5%AE%B9%E8%87%AA%E9%80%82%E5%BA%94/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可