兩個控件之間約束優(yōu)先級
以兩個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);
}];
默認(rèn)展示
設(shè)置label2為高優(yōu)先級
[label1 setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:(UILayoutConstraintAxisHorizontal)];
[label2 setContentHuggingPriority:UILayoutPriorityRequired forAxis:(UILayoutConstraintAxisHorizontal)];
如果文字變長了
label1.text = @"label1-123456789qwertyuiop";
label2.text = @"label2-123456789qwertyuiop";
默認(rèn)
設(shè)置壓縮label2
[label1 setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:(UILayoutConstraintAxisHorizontal)];
[label2 setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:(UILayoutConstraintAxisHorizontal)];