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);
}];
ios 使用Masonary创建等间距排列的控件
本文详细介绍了如何在iOS开发中利用Masonry库,通过Swift或Objective-C进行代码布局,实现控件的等间距排列。通过设置约束和更新约束,确保视图在不同屏幕尺寸下保持一致的间距效果。
摘要由CSDN通过智能技术生成