UIView
中關(guān)于 Content Hugging
和 Content Compression Resistance
的方法有:
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
在 Autolayout
優(yōu)先級的范圍是 1 ~ 1000
刹泄,創(chuàng)建一個約束绷跑,默認(rèn)的優(yōu)先級是最高的 1000
。
Content Hugging Priority
Content Hugging Priority: 該優(yōu)先級表示一個控件抗被拉伸的優(yōu)先級脊凰。優(yōu)先級越高,越不容易被拉伸钠右,默認(rèn)是251邪意。
使用場景:
當(dāng)一個視圖上有多個 intrinsic content size
的子控件,子視圖的總和丽蝎,不夠填充父視圖區(qū)域時猎拨,此屬性可以控制優(yōu)先拉伸哪個視圖內(nèi)容。
Content Compression Resistance Priority
Content Compression Resistance Priority: 該優(yōu)先級和上面那個優(yōu)先級相對應(yīng)屠阻,表示一個控件抗壓縮的優(yōu)先級红省。優(yōu)先級越高,越不容易被壓縮国觉,默認(rèn)是750吧恃。
使用場景:
當(dāng)一個視圖上有多個 intrinsic content size
的子控件,并且子控件可能會超出父視圖的區(qū)域時蛉加,此屬性可控制哪些視圖被內(nèi)容被優(yōu)先壓縮蚜枢,使其不超出父視圖區(qū)域。
舉例說明
Content Compression Resistance Priority
在 View
中添加了一個 UILabel
:
- (void)demo1 {
UILabel *yellowLabel = [[UILabel alloc] init];
yellowLabel.text = @"我是黃色Label,我是黃色Label,我是黃色Label,我是黃色Label";
yellowLabel.backgroundColor = [UIColor yellowColor];
[self.view addSubview:yellowLabel];
[yellowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.left.equalTo(self.view).offset(100);
make.right.equalTo(self.view).offset(-100);
}];
}
從最后的顯示效果來看针饥,中間的 Label
被壓縮了厂抽。因為左右約束的優(yōu)先級比固有內(nèi)容相關(guān)的優(yōu)先級要高,所以 Autolayout
布局的時候會優(yōu)先滿足左右兩個約束丁眼。這時候:左邊約束寬度 + 右邊約束寬度 + Label
的固有內(nèi)容寬度 > 屏幕寬度筷凤。所以最后只能壓縮 Label
顯示的寬度。
修改 View
左邊約束和右邊約束的優(yōu)先級,或者只修改左(右)邊約束優(yōu)先級藐守,然后設(shè)置 Label
抗壓縮的優(yōu)先級挪丢。
- (void)demo1 {
UILabel *yellowLabel = [[UILabel alloc] init];
yellowLabel.text = @"我是黃色Label,我是黃色Label";
yellowLabel.backgroundColor = [UIColor yellowColor];
[self.view addSubview:yellowLabel];
[yellowLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[yellowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.left.equalTo(self.view).offset(100).priority(250);
make.right.equalTo(self.view).offset(-100).priority(250);
}];
}
這時候 Label
控件的抗壓縮約束優(yōu)先級比右邊約束優(yōu)先級高,Autolayout
先滿足 Lable
控件的固有內(nèi)容 Size
的寬度卢厂,然后再滿足左邊和右邊約束乾蓬,表現(xiàn)出來就是 Lable
抗壓縮特性變強了,它更傾向于顯示它固有內(nèi)容 Size
慎恒,這時候被壓縮的就是左邊和右邊的約束任内。
Content Hugging Priority
在 View
中添加了一個 UILabel
:
- (void)demo2 {
UILabel *bluelabel = [[UILabel alloc] init];
bluelabel.text = @"我是藍色Label";
bluelabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:bluelabel];
[bluelabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.left.equalTo(self.view).offset(100);
make.right.equalTo(self.view).offset(-100);
}];
}
拉伸和壓縮的時候類似,左右約束優(yōu)先級比 Label
的 Content Hugging Priority
優(yōu)先級高融柬,并且此時:左邊約束寬度 + 右邊約束寬度+ Label
的固有內(nèi)容寬度 < 屏幕寬度死嗦。為了滿足左右兩個約束,就只有拉伸 Label
粒氧。
- (void)demo2 {
UILabel *bluelabel = [[UILabel alloc] init];
bluelabel.text = @"我是藍色Label";
bluelabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:bluelabel];
[bluelabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[bluelabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.left.equalTo(self.view).offset(100).priority(250);
make.right.equalTo(self.view).offset(-100).priority(250);
}];
}
這時候 Label
控件的抗拉伸約束優(yōu)先級比右邊約束優(yōu)先級高越除,Autolayout
先滿足 Lable
控件的固有內(nèi)容 Size
的寬度,然后再滿足左邊和右邊約束,表現(xiàn)出來就是 Lable
抗拉伸特性變強了,它更傾向于顯示它固有內(nèi)容 Size
,這時候被拉伸的就是左邊和右邊的約束。