[self.view addSubview:self.topScrollView];
UIImageView *lastIv = nil;
CGFloat ivW = 68 * kScaleFactor;
CGFloat ivH = 68 * kScaleFactor;
CGFloat padding = (kScreenW - 26 * kScaleFactor - ivW * 4) / 3;
for (int i = 0; i < 6; i++) {
UIImageView *iv = [[UIImageView alloc] init];
iv.backgroundColor = [UIColor redColor];
[self.topScrollView addSubview:iv];
[iv mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.topScrollView).offset(35 * kScaleFactor);
make.size.mas_equalTo(CGSizeMake(ivW, ivH));
if (lastIv) {
make.left.equalTo(lastIv.mas_right).offset(padding);
} else {
make.left.equalTo(self.topScrollView.mas_left).offset(13 * kScaleFactor);
}
}];
lastIv = iv;
}
[self.topScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(self.view);
make.height.mas_equalTo(@(139 * kScaleFactor));
make.right.mas_equalTo(lastIv.mas_right).offset(13 * kScaleFactor);
}];
现在遇到的情况如下:
topScrollView 为scrollView,将创建的scrollView添加到控制器的View中,注意是直接添加。
添加完毕之后,发现一个问题就是scrollView不能滚动。
采用了如下解决方案:
首先添加一个和scrollView一样大小的UIView.
UIView *bgView = [[UIView alloc] init];
[self.view addSubview:bgView];
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalTo(self.view);
make.height.mas_equalTo(@(139 * kScaleFactor));
}];
最后添加topScrollView的约束为:
[self.topScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(bgView);
make.right.mas_equalTo(lastIv.mas_right).offset(13 * kScaleFactor);
}];