美文网首页
Masonry约束优先级

Masonry约束优先级

作者: Thinkdifferents | 来源:发表于2017-05-28 00:36 被阅读0次

    两个控件之间约束优先级

    以两个label 为例

    UILabel *label1 = UILabel.new;
        label1.text = @"label1";
        label1.backgroundColor = [[UIColor alloc]initWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        [self.view addSubview:label1];
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10);
            make.top.offset(100);
        }];
        
        UILabel *label2 = UILabel.new;
        label2.text = @"label2";
        label2.backgroundColor = [[UIColor alloc]initWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        [self.view addSubview:label2];
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-10);
            make.centerY.equalTo(label1);
            make.left.mas_equalTo(label1.mas_right).offset(10);
        }];
    

    默认展示


    设置label2为高优先级

        [label1 setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:(UILayoutConstraintAxisHorizontal)];
        [label2 setContentHuggingPriority:UILayoutPriorityRequired forAxis:(UILayoutConstraintAxisHorizontal)];
    
    

    如果文字变长了

    
        label1.text = @"label1-123456789qwertyuiop";
        label2.text = @"label2-123456789qwertyuiop";
    
    

    默认


    设置压缩label2

        [label1 setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:(UILayoutConstraintAxisHorizontal)];
        [label2 setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:(UILayoutConstraintAxisHorizontal)];
    

    相关文章

      网友评论

          本文标题:Masonry约束优先级

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