美文网首页
Masonry 优先级的坑

Masonry 优先级的坑

作者: 七维树 | 来源:发表于2021-05-28 10:39 被阅读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)];
    

    相关文章

      网友评论

          本文标题:Masonry 优先级的坑

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