当前位置: 代码迷 >> 综合 >> masonry 在UIScrollview上布局
  详细解决方案

masonry 在UIScrollview上布局

热度:32   发布时间:2023-12-17 02:41:50.0
问题:

直接在scrollview上addSubview,往往出偏差

解决办法:

是加一层容器containerView做间隔,containerView add到scrollview上,自定义的子view add到containerView上

然后重点是:在containerView的mas设置block中

需要竖向滑动时设置 width
make.width.equalTo(scrollView);
需要横向滑动时设置 height
make.height.equalTo(scrollView);

示例
- (void)setupUI {
    //scrollview加到controller的self.view上[self.view addSubview:self.backgroundScrollView];//容器containerView加到scrollview上[self.backgroundScrollView addSubview:self.containerView];//子view加到容器containerView上[self.containerView addSubview:self.subViewA];[self.containerView addSubview:self.subViewB];
}- (void)addConstraints {
        [self.backgroundScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.right.equalTo(self.view);make.bottom.equalTo(self.view).offset(-TAB_BAR_HEIGHT);}];[self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.bottom.right.equalTo(self.backgroundScrollView);//主要是这里,如下是竖向滑动,就设置width同scrollviewmake.width.equalTo(self.backgroundScrollView);}];[self.subViewA mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.right.equalTo(self.containerView);}];[self.subViewB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.subViewA.mas_bottom).offset(16);make.left.right.bottom.equalTo(self.containerView);}];
}