Masonry icon indicating copy to clipboard operation
Masonry copied to clipboard

多控件居中

Open Wenjie-Qin opened this issue 6 years ago • 1 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. ⚠️

Wenjie-Qin avatar Jul 15 '19 12:07 Wenjie-Qin

https://github.com/SnapKit/Masonry/pull/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;
}

cntrump avatar Jun 01 '21 23:06 cntrump