Masonry 优先级的坑

2021-05-28  本文已影响0人  七维树

需求是这样的,一个view最大宽度不超过640,同时距离左右两边间距最少要24

//错误的写法

[view mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@(0));
        make.width.equalTo(@(640)).priorityLow();
        make.left.lessThanOrEqualTo(@(24)).priorityHigh();  //这里加了High优先级会出问题
        make.right.lessThanOrEqualTo(@(-24)).priorityHigh();
 }];

正确的写法一,不要加priorityHigh()这个优先级,只用Low就可以

[view mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@(0));
        make.width.equalTo(@(640)).priorityLow();
        make.left.lessThanOrEqualTo(@(24));
        make.right.lessThanOrEqualTo(@(-24));
 }];

正确的写法二

[view mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@(0));
        make.width.equalTo(@(640)).priorityLow();
        make.left.lessThanOrEqualTo(@(24)).priorityHigh();
        make.right.lessThanOrEqualTo(@(-24)).priorityHigh();
 }];
//在约束外部,给view一个压缩方向的优先级
 [view setContentCompressionResistancePriority:(UILayoutPriorityDefaultLow) forAxis:(UILayoutConstraintAxisHorizontal)];
上一篇下一篇

猜你喜欢

热点阅读