循环创建多个view 是在简单不过的事 但是多数时间建立约束时 都是用的init方法 这里记录一下用Masonry 建立约束的方法
- 先创建装载的“容器”
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:_scrollView];
[_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view.mas_centerX);
make.centerY.equalTo(self.view.mas_centerY).with.offset(-50);
make.width.equalTo(@(200));
make.height.equalTo(@(200));
}];
_scrollView.backgroundColor = [UIColor lightGrayColor];
- 自定义view
需要循环创建的view 多数情况下是都是传入自定义的view
这里 就用这个代替
//此处可以传view数组
NSMutableArray * viewARR = [NSMutableArray array];
for (int a = 0; a < 3; a ++) {
UIView * arrview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
arrview.backgroundColor = [UIColor whiteColor];
[viewARR addObject:arrview];
}
- 建立约束
左右滚动
UIView * contenview = [UIView new];
[_scrollView addSubview:contenview];
[contenview mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo (_scrollView);
make.height.equalTo (_scrollView); 此处没有设置宽的约束
}];
//关于这个延迟 因为masory创建约束之后 获取的frame 是(0,0,0,0)但是有一个延迟的话 才能获取真是的view的大小 延迟时间改成0 也可以
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"******%@",contenview);
});
UIView * lastView ;
//颜色数组
NSArray * colors = @[ [UIColor purpleColor] , [UIColor orangeColor], [UIColor redColor]];
for (int i = 0; i < viewARR.count ; i++) {
UIView * view = viewARR[i];
[contenview addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo (contenview.mas_top);
make.bottom.equalTo (contenview.mas_bottom);
make.left.equalTo(lastView ? lastView.mas_right: @0).offset(10);
make.width.equalTo (_scrollView.mas_width);
}];
view.backgroundColor = colors[i];
lastView = view;
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@"=======%d___%@",i,lastView);
// });
}
[contenview mas_updateConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(lastView.mas_right);
}];
上下滚动
UIView * contenview = [UIView new];
[_scrollView addSubview:contenview];
[contenview mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo (_scrollView);
make.width.equalTo (_scrollView);
}];
//关于这个延迟 因为masory创建约束之后 获取的frame 是(0,0,0,0)但是有一个延迟的话 才能获取真是的view的大小 延迟时间改成0 也可以
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@"******%@",contenview);
// });
UIView * lastView ;
//颜色数组
NSArray * colors = @[ [UIColor purpleColor] , [UIColor orangeColor], [UIColor redColor]];
for (int i = 0; i < viewARR.count ; i++) {
UIView * view = viewARR[i];
[contenview addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo (contenview.mas_left);
make.right.equalTo (contenview.mas_right);
make.top.equalTo(lastView ? lastView.mas_bottom: @0).offset(10);
make.height.equalTo (_scrollView.mas_height);
}];
view.backgroundColor = colors[i];
lastView = view;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"=======%d___%@",i,lastView);
});
}
[contenview mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
其实就是基于Masonry 和三目运算实现 (lastView ? lastView.mas_right: @0).offset(10)