<h3 id=ConstraintPriority>约束优先级问题</h3>
<h4 id=UseMasonry>使用Masonry</h4>
一行两个
Label
,距离屏幕左右边距各15
,测试不同文字内容长度时Label
的显示情况,以及通过设置优先级后Label
的显示情况来透彻理解约束优先级问题。
ContentHuggingPriority
表示当前的Label的内容不想被拉伸;HuggingPriority
== 250ContentCompressionResistancePriority
表示当前的Label的内容不想被收缩;CompressionResistancePriority
== 750@interface ViewController ()
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;
@end
#pragma mark - Getters and Setters
- (UILabel *)leftLabel {
if (_leftLabel == nil) {
_leftLabel = [[UILabel alloc] init];
_leftLabel.text = @"左边";
_leftLabel.textColor = [UIColor redColor];
_leftLabel.backgroundColor = [UIColor lightGrayColor];
}
return _leftLabel;
}
- (UILabel *)rightLabel {
if (_rightLabel == nil) {
_rightLabel = [[UILabel alloc] init];
_rightLabel.text = @"右边";
_rightLabel.textColor = [UIColor blueColor];
_rightLabel.backgroundColor = [UIColor lightGrayColor];
}
return _rightLabel;
}
#pragma mark - Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}
#pragma mark - SetupUI
- (void)setupUI {
[self.view addSubview:self.leftLabel];
[self.view addSubview:self.rightLabel];
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(16);
make.top.equalTo(self.view).offset(50);
}];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-16);
make.centerY.equalTo(self.leftLabel);
}];
}
<h5>文字内容足够少、居左右两侧显示</h5>
按照上面默认的即可。
<h5>文字内容足够少、但需要一方拉伸</h5>
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(16);
make.top.equalTo(self.view).offset(50);
make.right.equalTo(self.rightLabel.mas_left).offset(-10);
}];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-16);
make.centerY.equalTo(self.leftLabel);
}];
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(16);
make.top.equalTo(self.view).offset(50);
}];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-16);
make.centerY.equalTo(self.leftLabel);
make.left.equalTo(self.leftLabel.mas_right).offset(10);
}];
或者按照如下方式设置
/**
* 设置左侧 Label 不被拉伸(即右边的被拉伸)
* 默认情况下:HuggingPriority == 250;
* 就要设置 leftLabel.HuggingPriority > 250
* 或者 rightLabel.HuggingPriority < 250
* UILayoutPriorityRequired == 1000;
* 这里只要是 HuggingPriority > 250 即可,写成 251 也是同样的效果;
*/
[self.leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
// UILayoutPriorityFittingSizeLevel == 50;
// [self.rightLabel setContentHuggingPriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(16);
make.top.equalTo(self.view).offset(50);
make.right.equalTo(self.rightLabel.mas_left).offset(-10);
}];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-16);
make.centerY.equalTo(self.leftLabel);
make.left.equalTo(self.leftLabel.mas_right).offset(10);
}];
<h5>文字内容足够多、右侧被压缩</h5>
只要设置leftLabel
的right
距rightLabel
的mas_left
间距即可。需要谁不被压缩,就设置谁距离另外一个被压缩的的间距即可。
如果左右相互之间的间距都设置的话,那么默认右侧
Label
被压缩。
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(16);
make.top.equalTo(self.view).offset(50);
make.right.equalTo(self.rightLabel.mas_left).offset(-10);
}];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-16);
make.centerY.equalTo(self.leftLabel);
}];
<h5>文字内容足够多、左侧被压缩</h5>
设置rightLabel
的left
距leftLabel
的mas_right
间距即可。需要谁不被压缩,就设置谁距离另外一个被压缩的的间距即可。
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(16);
make.top.equalTo(self.view).offset(50);
}];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-16);
make.centerY.equalTo(self.leftLabel);
make.left.equalTo(self.leftLabel.mas_right).offset(10);
}];
或者通过ContentCompressionResistancePriority
设置
/**
* 设置右侧 Label 不被压缩(即左侧的被压缩)
* 默认情况下:ContentCompressionResistancePriority == 750;
* 就要设置 rightLabel.ContentCompressionResistancePriority > 750
* 或者 leftLabel.ContentCompressionResistancePriority < 750
* UILayoutPriorityRequired == 1000;
* 这里只要是 ContentCompressionResistancePriority > 750 即可,写成 751 也是同样的效果;
*/
[self.rightLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
// UILayoutPriorityDefaultLow == 250;
[self.leftLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(16);
make.top.equalTo(self.view).offset(50);
make.right.equalTo(self.rightLabel.mas_left).offset(-10);
}];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-16);
make.centerY.equalTo(self.leftLabel);
make.left.equalTo(self.leftLabel.mas_right).offset(10);
}];