多控件居中
Wenjie-Qin opened this issue · comments
Wenjie-Qin commented
New Issue Checklist
- I have looked at the Documentation
- I have filled out this issue template.
Issue Info
Info | Value |
---|---|
Platform | e.g. ios/osx/tvos |
Platform Version | e.g. 8.0 |
Masonry Version | e.g. 1.0 |
Integration Method | e.g. carthage/cocoapods/manually |
Issue Description
cntrump commented
UILayoutGuide *contentLayoutGuide = [[UILayoutGuide alloc] init];
[self.view addLayoutGuide:contentLayoutGuide];
[contentLayoutGuide mas_makeConstraints:^(id<MASLayoutConstraint> _Nonnull make) {
make.center.mas_equalTo(0);
make.width.mas_equalTo(self.view);
}];
NSArray<UIColor *> *colors = @[
UIColor.redColor,
UIColor.yellowColor,
UIColor.blueColor,
UIColor.greenColor
];
UIView *prev = nil;
NSInteger count = 4;
for (NSInteger i = 0; i < count; i++) {
UIView *v = [[UIView alloc] init];
v.backgroundColor = colors[i % 4];
[self.view addSubview:v];
[v mas_makeConstraints:^(MASConstraintMaker *make) {
if (i == 0) {
make.top.mas_equalTo(contentLayoutGuide).offset(24);
} else {
make.top.mas_equalTo(prev.mas_bottom).offset(8);
}
make.centerX.equalTo(contentLayoutGuide);
make.height.mas_equalTo(80);
make.width.mas_equalTo(contentLayoutGuide).offset(-24);
if (i == count - 1) {
make.bottom.mas_equalTo(contentLayoutGuide).offset(-24);
}
}];
prev = v;
}