使用masonry对多个控件同时布局
实现效果如下图中的底部label,都是水平布局,宽度固定,均分整个屏幕。
具体实现
我们需要先将要进行均匀布局的视图放到一个数组中,然后通过Masonry为NSArray添加的一个分类:NSArray+MASAdditions.h调用数组的一个方法。
self.norLab = [[UILabel alloc] initWithFrame:CGRectZero];
self.norLab.font = FONT12;
self.norLab.text = @"正常";
self.norLab.textAlignment = NSTextAlignmentCenter;
self.norLab.textColor = [UIColor whiteColor];
[self addSubview:self.norLab];
self.latLab = [[UILabel alloc] initWithFrame:CGRectZero];
self.latLab.font = FONT12;
self.latLab.text = @"迟到";
self.latLab.textAlignment = NSTextAlignmentCenter;
self.latLab.textColor = [UIColor whiteColor];
[self addSubview:self.latLab];
self.leaveLab = [[UILabel alloc] initWithFrame:CGRectZero];
self.leaveLab.font = FONT12;
self.leaveLab.text = @"早退";
self.leaveLab.textColor = [UIColor whiteColor];
self.leaveLab.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.leaveLab];
self.absentLab = [[UILabel alloc] initWithFrame:CGRectZero];
self.absentLab.font = FONT12;
self.absentLab.text = @"缺卡";
self.absentLab.textColor = [UIColor whiteColor];
self.absentLab.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.absentLab];
NSArray *views = @[self.norLab, self.latLab, self.leaveLab, self.absentLab];
[self.labArr addObjectsFromArray:views];
[views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:80 leadSpacing:10 tailSpacing:10];
[views mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom).with.offset(-10);
make.height.equalTo(@15);
}];
[views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:80 leadSpacing:10 tailSpacing:10];
API解读MASAxisTypeHorizontal代表水平布局。withFixedItemLength固定宽度。 leadSpacing最左侧视图距离父视图左边距,tailSpacing最右侧视图距离父视图右边距