美文网首页
Masonry使用

Masonry使用

作者: 泡芙coder | 来源:发表于2021-07-16 15:39 被阅读0次

基本本实用方式

 [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self).offset(TheSQTheme.space1);

        make.centerY.mas_equalTo(self);

    }];

    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.mas_equalTo(self).offset(-TheSQTheme.space1);

        make.centerY.mas_equalTo(self);

        make.height.width.mas_equalTo(15);

    }];

    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.mas_equalTo(self).offset(-TheSQTheme.space1);

        make.centerY.mas_equalTo(self);

        make.height.mas_equalTo(25);

    }];

更新UI

如果你已经设置一个控件的坐标了

 [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.mas_equalTo(self).offset(-TheSQTheme.space1);

        make.centerY.mas_equalTo(self);

        make.left.mas_equalTo(self.titleLabel.mas_right).offset(10);

        make.height.mas_equalTo(self);

    }];

需要根据下载下来的数据再次调整self.textField
 [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {

            make.right.mas_equalTo(self).offset(-15);

            make.centerY.mas_equalTo(self);

            make.left.mas_equalTo(self.titleLabel.mas_right).offset(10);

            make.height.mas_equalTo(50);

        }];

或者可以使用mas_updateConstraints。

mas_remakeConstraints 和 mas_updateConstraints 区别:

这两个函数都会更新控件约束,但是mas_remakeConstraints 会删除原来的约束,mas_updateConstraints不会删除。

设置约束优先级

我们一般用以下两个函数实现:
-(void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axisNS_AVAILABLE_IOS(6_0); // 设置抗拉伸优先级

-(void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axisNS_AVAILABLE_IOS(6_0); // 设置抗压缩优先级

抗拉伸函数的使用情况一般是宽度足够的时候,优先级设置的高一点控件就不会拉长有多余的空白地方。

抗压缩函数使用是在宽度不够的情况下,优先级设置的高一点会优先展示全部内容,而不会被压缩。

优缺点

masonry 是相对布局,和直接setframe绝对布局方式相比使用起来更加方便,但是有时候使用不当会造成内存持续增加,

 存储比如一个控件更新布局的时候没有用mas_updateConstraints,还是用mas_makeConstraints。

相关文章

网友评论

      本文标题:Masonry使用

      本文链接:https://www.haomeiwen.com/subject/qlrbpltx.html