ios 使用Masonary创建等间距排列的控件
程序员文章站
2022-05-30 13:45:59
...
UIView *backView = [[UIView alloc] init];
backView.backgroundColor = [UIColor grayColor];
[self.view addSubview:backView];
[backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(self.view).with.offset(100);
make.height.mas_equalTo(100);
}];
NSMutableArray *array = [NSMutableArray array];
for(NSInteger i=0;i<5;i++){
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor colorWithRed:(arc4random()%256)/255.0 green:(arc4random()%256)/255.0 blue:(arc4random()%256)/255.0 alpha:1.0];
[backView addSubview:view];
[array addObject:view];
}
//FixedSpacing:间距
//leadSpacing:最左边控件距父视图左边间距
//tailSpacing:最右边控件距父视图右边间距
//MASAxisTypeVertical 控件垂直排列
//MASAxisTypeHorizontal 控件水平排列
[array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:5 leadSpacing:60 tailSpacing:5];
[array mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@10);
make.bottom.equalTo(@-10);
}];