視圖水平或者垂直方向布局子空間
1.視圖尺寸自適應达址,視圖之間邊距固定
2.視圖尺寸固定作彤,視圖之間邊距自適應
對于上述兩種需求第练,Masonry
給系統(tǒng)NSArray
增加分類View+MASAdditions
擴充API
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;
示例
1二驰、創(chuàng)建視圖添加到父視圖上,同時將視圖添加到數(shù)組中
UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor blueColor];
UIView *view3 = [[UIView alloc] init];
view3.backgroundColor = [UIColor greenColor];
[self.view addSubview:view1];
[self.view addSubview:view2];
[self.view addSubview:view3];
NSArray *views = @[view1, view2, view3];
2.1稿静、水平方向 視圖間距固定 寬度自適應
[views mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:10 tailSpacing:10];
[views mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@200.0f);
make.height.equalTo(@100.0f);
}];
image.png
2.2梭冠、垂直方向 視圖間距固定 寬度自適應
[views mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:50 leadSpacing:200 tailSpacing:50];
[views mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.width.equalTo(@100.0f);
}];
example.png
需要注意的是: 橫排的時候要相應設(shè)置控件數(shù)組的垂直約束,豎排的時候要相應設(shè)置控件數(shù)字的水平約束改备。