SnapKit / Masonry

Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout
MIT License
18.06k stars 3.15k forks source link

Fully support NS/UILayoutGuide and SwiftPM #594

Closed cntrump closed 3 years ago

cntrump commented 3 years ago

Example frameLayoutGuide & contentLayoutGuide:

_scrollView = [[UIScrollView alloc] init];
[self.view addSubview:_scrollView];
[_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(0);
}];

NSArray<UIColor *> *colors = @[
    UIColor.redColor,
    UIColor.yellowColor,
    UIColor.blueColor,
    UIColor.greenColor
];

UILayoutGuide *frameLayoutGuide = _scrollView.frameLayoutGuide;
UILayoutGuide *contentLayoutGuide = _scrollView.contentLayoutGuide;
[contentLayoutGuide mas_makeConstraints:^(id<MASLayoutConstraint>  _Nonnull make) {
    make.width.mas_equalTo(frameLayoutGuide);
}];

UIView *prev = nil;
NSInteger count = 40;

for (NSInteger i = 0; i < count; i++) {
    UIView *v = [[UIView alloc] init];
    v.backgroundColor = colors[i % 4];
    [_scrollView addSubview:v];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        if (i == 0) {
            make.top.mas_equalTo(contentLayoutGuide).mas_offset(24);
        } else {
            make.top.mas_equalTo(prev.mas_bottom).mas_offset(8);
        }

        make.centerX.mas_equalTo(contentLayoutGuide);

        make.height.mas_equalTo(80);
        make.width.mas_equalTo(frameLayoutGuide).mas_offset(-24);

        if (i == count - 1) {
            make.bottom.mas_equalTo(contentLayoutGuide).mas_offset(-24);
        }
    }];

    prev = v;
}
cntrump commented 3 years ago

Example center two labels:

UILayoutGuide *box = [[UILayoutGuide alloc] init];
[self.view addLayoutGuide:box];
[box mas_makeConstraints:^(id<MASLayoutConstraint> _Nonnull make) {
    make.centerX.mas_equalTo(self.view.mas_centerX);
    make.centerY.mas_equalTo(self.view.mas_centerY);
}];

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleLargeTitle];
titleLabel.text = @"title";
[self.view addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(box.mas_top);
    make.centerX.mas_equalTo(box.mas_centerX);
}];

UILabel *descrLabel = [[UILabel alloc] init];
descrLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
descrLabel.text = @"descr";
[self.view addSubview:descrLabel];
[descrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.mas_equalTo(box.mas_centerX);
    make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(8);
    make.bottom.mas_equalTo(box.mas_bottom);
}];

Or more simple:

UILayoutGuide *box = [[UILayoutGuide alloc] init];
[self.view addLayoutGuide:box];
[box mas_makeConstraints:^(id<MASLayoutConstraint> _Nonnull make) {
    make.center.mas_equalTo(0);
}];

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleLargeTitle];
titleLabel.text = @"title";
[self.view addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(box);
    make.centerX.mas_equalTo(box.mas_centerX);
}];

UILabel *descrLabel = [[UILabel alloc] init];
descrLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
descrLabel.text = @"descr";
[self.view addSubview:descrLabel];
[descrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.mas_equalTo(box);
    make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(8);
    make.bottom.mas_equalTo(box);
}];
cntrump commented 3 years ago

All done.

cntrump commented 3 years ago

Example UITableViewCell with UILayoutGuide

@implementation Cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UILayoutGuide *box = [[UILayoutGuide alloc] init];
        [self.contentView addLayoutGuide:box];
        [box mas_makeConstraints:^(MASConstraintMaker * _Nonnull make) {
            make.edges.mas_equalTo(self.contentView.mas_safeAreaLayoutGuide).inset(24);
        }];

        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleLargeTitle];
        titleLabel.text = @"title";
        [self.contentView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(box);
        }];

        UILabel *descrLabel = [[UILabel alloc] init];
        descrLabel.numberOfLines = 0;
        descrLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
        descrLabel.text = @"descrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescrdescr";
        [self.contentView addSubview:descrLabel];
        [descrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(8);
            make.left.right.bottom.mas_equalTo(box);
        }];
    }

    return self;
}
cntrump commented 3 years ago

Transfer to https://github.com/SnapKit/Masonry/pull/595