Transfer from http://blog.sina.com.cn/s/blog_13fd67a560102wil5.html
Used to record the last created Uilabel
UILabel *lastlabel = nil;
for (int i = 0; i < 9; i++) {
UILabel *label = [UILabel new];
Label.textalignment = Nstextalignmentcenter;
Label.text = [NSString stringwithformat:@ "~%d~", I];
[Self.view Addsubview:label];
[Label mas_makeconstraints:^ (Masconstraintmaker *make) {
Set height
Make.height.offset (40.0f);
Calculates the formula at the top of the distance 60 = the height of the previous distance above the top + Uilabel
Float Coltop = (+ I/3 * 60.0f);
Make.top.offset (Coltop);
When the left column is the distance from the parent view to the left 20
if (i%3 = = 0) {
Make.left.offset (20.0f);
}else{
When the middle column was on the right side of the previous Uilabel, add 20 distances and set the equal height
Make.width.equalTo (Lastlabel.mas_width);
Make.left.equalTo (lastlabel.mas_right). Offset (20.0f);
}
//When it is the rightmost column, the distance from the right parent view is 20 because it is left so 20 control bottom is negative!
if (i%3 = = 2) {
Make.right.offset ( -20.0f);
}
}];
LastLabel = label;
}
iOS Development Adaptive Screen IX Gongge (third-party library masonry)