Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully support NS/UILayoutGuide and SwiftPM #594

Closed
wants to merge 18 commits into from

Conversation

cntrump
Copy link
Contributor

@cntrump cntrump commented May 30, 2021

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
Copy link
Contributor Author

cntrump commented May 30, 2021

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 cntrump force-pushed the master branch 3 times, most recently from e983a38 to 2dfd239 Compare May 30, 2021 16:05
@cntrump
Copy link
Contributor Author

cntrump commented May 30, 2021

All done.

@cntrump cntrump changed the title Add MASLayoutGuide for UI/NSLayoutGuide support Fully support UI/NSLayoutGuide May 31, 2021
@cntrump cntrump changed the title Fully support UI/NSLayoutGuide Fully support NS/UILayoutGuide May 31, 2021
@cntrump cntrump mentioned this pull request May 31, 2021
@cntrump cntrump force-pushed the master branch 2 times, most recently from 75709d2 to 25fdb85 Compare May 31, 2021 14:56
@cntrump cntrump force-pushed the master branch 2 times, most recently from a9beeeb to af0bde8 Compare May 31, 2021 22:54
@cntrump
Copy link
Contributor Author

cntrump commented Jun 1, 2021

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 cntrump changed the title Fully support NS/UILayoutGuide Fully support NS/UILayoutGuide and SwiftPM Jun 1, 2021
@cntrump cntrump mentioned this pull request Jun 1, 2021
2 tasks
@cntrump
Copy link
Contributor Author

cntrump commented Jun 2, 2021

Transfer to #595

@cntrump cntrump closed this Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant