Closed cntrump closed 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);
}];
All done.
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;
}
Transfer to https://github.com/SnapKit/Masonry/pull/595
Example
frameLayoutGuide
&contentLayoutGuide
: