有時(shí)我們在給UILabel或者UITextField這種帶IntrinsicHeight和IntrinsicWidth類型的視圖做約束時(shí)恤磷,約束的結(jié)果會(huì)出乎我們的意料。
例如下面這樣:
UILabel *label1 = [[UILabel alloc] init];
label1.text = @"我是label1";
label1.textColor = [UIColor blackColor];
label1.backgroundColor = [UIColor purpleColor];
[self.view addSubview:label1];
UILabel *label2 = [[UILabel alloc] init];
label2.text = @"我是label2";
label2.backgroundColor = [UIColor yellowColor];
label2.textColor = [UIColor blackColor];
[self.view addSubview:label2];
[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.top.equalTo(self.view);
}];
[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view);
make.top.equalTo(self.view);
make.left.equalTo(label1.mas_right);
}];
561534842242_.pic.jpg
因?yàn)閁ILabel默認(rèn)的hugging優(yōu)先級(jí)是250,當(dāng)2個(gè)UILabel或者UITextField視圖需要拉伸或者壓縮時(shí),系統(tǒng)不知道該拉伸誰或者壓縮誰,這時(shí)需要我們手動(dòng)的設(shè)置優(yōu)先級(jí)骨稿。
上面的情況我們可以設(shè)置下label1的HuggingPriority更高,那么系統(tǒng)會(huì)優(yōu)先壓縮label1姜钳。
[label1 setContentHuggingPriority:UILayoutPriorityDefaultHigh
forAxis:UILayoutConstraintAxisHorizontal];
同理坦冠,設(shè)置CompressionResistancePriority級(jí)別更低也是可以的。設(shè)置完后的效果是下面這樣的傲须。
551534842145_.pic.jpg