确定间距 等间距布局
/**
* 确定间距 等间距布局
*
* @param axisType 布局方向
* @param fixedSpacing 两个item之间的间距(最左面的item的左边, 最右边item的右边都不是这个)
* @param leadSpacing 第一个item到父视图边距
* @param tailSpacing 最后一个item到父视图边距
*/
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;
//在红色View里面放三个正方形View, 等间距为10
NSInteger padding = 10;
UIView *yellowView1 = [[UIView alloc] init];
yellowView1.backgroundColor = [UIColor yellowColor];
[redView addSubview:yellowView1];
UIView *yellowView2 = [[UIView alloc] init];
yellowView2.backgroundColor = [UIColor yellowColor];
[redView addSubview:yellowView2];
UIView *yellowView3 = [[UIView alloc] init];
yellowView3.backgroundColor = [UIColor yellowColor];
[redView addSubview:yellowView3];
[@[yellowView1, yellowView2, yellowView3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:padding leadSpacing:padding tailSpacing:padding];
[@[yellowView1, yellowView2, yellowView3] mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(redView).offset(10);
make.height.mas_equalTo(yellowView3.mas_width);
}];
确定控件大小 等间距布局
/**
* distribute with fixed item size
*
* @param axisType 布局方向
* @param fixedItemLength 每个item的布局方向的长度
* @param leadSpacing 第一个item到父视图边距
* @param tailSpacing 最后一个item到父视图边距
*/
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;
//在红色View里面放三个正方形蓝色View, 宽度均为30, 间隙一样大
NSMutableArray *blueViews = [NSMutableArray array];
for (NSInteger i = 0; i < 3; i++) {
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[redView addSubview:blueView];
[blueViews addObject:blueView];
}
CGFloat padding2 = (300 - 3 * 30) / 4;
[blueViews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:30 leadSpacing:padding2 tailSpacing:padding2];
[blueViews mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(redView);
UIView *blueView = (UIView *)blueViews[0];
make.height.mas_equalTo(blueView.mas_width);
}];
网友评论