目的:
- 利用UIScrollView中利用Masonry布局多个相同的子控件
@property (nonatomic, strong) UIScrollView * channelScrollView;
- (void)initUI {
self.channelScrollView = [[UIScrollView alloc] init];
self.channelScrollView.backgroundColor = [UIColor yellowColor];
self.channelScrollView.pagingEnabled = NO;
[self.channelView addSubview:self.channelScrollView];
}
- (void)initLayout {
UILabel *lastLabel = nil;
NSArray *titleArray = @[@"热门",
@"热门",
@"热门",
@"热门",
@"热门",
@"热门",
@"热门",
@"热门",
@"热门",
@"热门",
@"热门"];
for (NSUInteger i = 0; i < titleArray.count; ++i) {
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0;
label.text = titleArray[i];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = CYRandomColor;
[self.channelScrollView addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(20);
make.width.mas_equalTo(70);
make.height.mas_equalTo(70);
if (lastLabel) {
make.left.mas_equalTo(lastLabel.mas_right).offset(12);
} else {
make.left.mas_equalTo(self.channelScrollView).offset(15);
}
}];
lastLabel = label;
}
[self.channelScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(self.view.mas_width);
make.top.mas_equalTo(self.channelLabel.mas_bottom).offset(0);
make.bottom.offset(0);
make.right.mas_equalTo(lastLabel.mas_right).offset(15);
}];
}
make.left.mas_equalTo(lastLabel.mas_right).offset(12);
- 设置子控件之间的间距
make.left.mas_equalTo(self.channelScrollView).offset(15);
- 设置子控件跟父控件的起始边距
make.right.mas_equalTo(lastLabel.mas_right).offset(15);
设置channelScrollView 可以滑动的最大宽度距离