Masonary是一个给控件添加约束条件的第三方库,它是轻量级的布局框架。它和autolayout作用差不多。
支持一下属性
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
//添加新的约束条件
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(200, 200));
}];
UIScrollView *scrollView = [UIScrollView new];
scrollView.backgroundColor = [UIColor blueColor];
[view addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(view).with.insets(UIEdgeInsetsMake(5, 5, 5, 5));
}];
UIView *container = [UIView new];
container.backgroundColor = [UIColor blackColor];
[scrollView addSubview:container];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.width.equalTo(scrollView);
}];
int count = 10;
UIView *lastView = nil;
for (int i = 1; i <= count; ++i) {
UIView *subv = [UIView new];
[container addSubview:subv];
subv.backgroundColor = [UIColor colorWithHue:arc4random() % 256/256.0 saturation:arc4random() % 128/256.0 + 0.5 brightness:arc4random() % 128/256.0 + 0.5 alpha:1];
[subv mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(container);
make.height.mas_equalTo(@(20*i));
if (lastView) {
make.top.mas_equalTo(lastView.mas_bottom);
}
else{
make.top.mas_equalTo(container.mas_top);
}
}];
lastView = subv;
}
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
原文:http://www.cnblogs.com/cainiao-blogs/p/4191380.html