SnapKit / Masonry

Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

多控件居中

Wenjie-Qin opened this issue · comments

New Issue Checklist

🚫 If this template is not filled out your issue will be closed with no comment. 🚫

  • 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

⚠️ Replace this with the description of your issue. ⚠️

#594

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;
}