/**
* axisType 轴线方向
* fixedSpacing 间隔大小
* fixedItemLength 每个控件的固定长度/宽度
* leadSpacing 头部间隔
* tailSpacing 尾部间隔 * *///
1. 等间隔排列 - 多个控件间隔固定,控件长度/宽度变化
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisTypewithFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacingtailSpacing:(CGFloat)tailSpacing;//
2. 等间隔排列 - 多个固定大小固定,间隔空隙变化
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisTypewithFixedItemLength:(CGFloat)fixedItemLengthleadSpacing:(CGFloat)leadSpacingtailSpacing:(CGFloat)tailSpacing;
所以也就知道了, 将fixedSpacing, leadSpacing, tailSpacing都赋值同一个间距, 数组内的的View就会自动计算出宽度, 完成水平方向的布局.
要注意的是, 这个方法仅仅完成了水平方向的布局, 如果想确定这几个View的位置, 还需要指定竖直方向位置和高度, 这里可以用数组直接调用 mas_makeConstraints:^(MASConstraintMaker *make){} 完成布局.
代码示例:
UIView*backView=[UIView new];
backView.backgroundColor=[UIColor redColor];
[self.view addSubview:backView];
[backView mas_makeConstraints:^(MASConstraintMaker*make){
make.center.mas_equalTo(backView.center);
make.size.mas_equalTo(CGSizeMake(250,300));
}];
//水平排列//在backview里面放n个蓝色方块,宽度均为30, 间隙一样大,n大于2啊,就一个还等个毛线啊
intnum=4;
NSMutableArray*blueViews=[NSMutableArray array];
for(inti=0;i UIView*blueView=[UIView new]; blueView.backgroundColor=[UIColor blueColor]; [backView addSubview:blueView]; [blueViews addObject:blueView]; } CGFloat padding=(250-num*30)/(num+1); [blueViews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:30leadSpacing:padding tailSpacing:padding]; [blueViews mas_makeConstraints:^(MASConstraintMaker*make){ make.centerY.equalTo(backView); UIView*blueView=(UIView*)blueViews[0]; make.height.mas_equalTo(blueView.mas_width); }]; //竖直排列NSMutableArray*yellowViews=[NSMutableArray array]; for(inti=0;i UIView*yellowView=[UIView new]; yellowView.backgroundColor=[UIColor yellowColor]; [backView addSubview:yellowView]; [yellowViews addObject:yellowView]; } [yellowViews mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:30leadSpacing:padding tailSpacing:padding]; [yellowViews mas_makeConstraints:^(MASConstraintMaker*make){ make.centerX.mas_equalTo(backView.mas_centerX); make.width.mas_equalTo(30); }];