UIView
中关于 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
优先级的范围是 1 ~ 1000
,创建一个约束,默认的优先级是最高的 1000
。
Content Hugging Priority
Content Hugging Priority: 该优先级表示一个控件抗被拉伸的优先级。优先级越高,越不容易被拉伸,默认是251。
使用场景:
当一个视图上有多个 intrinsic content size
的子控件,子视图的总和,不够填充父视图区域时,此属性可以控制优先拉伸哪个视图内容。
Content Compression Resistance Priority
Content Compression Resistance Priority: 该优先级和上面那个优先级相对应,表示一个控件抗压缩的优先级。优先级越高,越不容易被压缩,默认是750。
使用场景:
当一个视图上有多个 intrinsic content size
的子控件,并且子控件可能会超出父视图的区域时,此属性可控制哪些视图被内容被优先压缩,使其不超出父视图区域。
举例说明
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
被压缩了。因为左右约束的优先级比固有内容相关的优先级要高,所以 Autolayout
布局的时候会优先满足左右两个约束。这时候:左边约束宽度 + 右边约束宽度 + Label
的固有内容宽度 > 屏幕宽度。所以最后只能压缩 Label
显示的宽度。
修改 View
左边约束和右边约束的优先级,或者只修改左(右)边约束优先级,然后设置 Label
抗压缩的优先级。
- (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
控件的抗压缩约束优先级比右边约束优先级高,Autolayout
先满足 Lable
控件的固有内容 Size
的宽度,然后再满足左边和右边约束,表现出来就是 Lable
抗压缩特性变强了,它更倾向于显示它固有内容 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);
}];
}
拉伸和压缩的时候类似,左右约束优先级比 Label
的 Content Hugging Priority
优先级高,并且此时:左边约束宽度 + 右边约束宽度+ Label
的固有内容宽度 < 屏幕宽度。为了满足左右两个约束,就只有拉伸 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
控件的抗拉伸约束优先级比右边约束优先级高,Autolayout
先满足 Lable
控件的固有内容 Size
的宽度,然后再满足左边和右边约束,表现出来就是 Lable
抗拉伸特性变强了,它更倾向于显示它固有内容 Size
,这时候被拉伸的就是左边和右边的约束。