概念简述
捆绑优先级: 在Autolayout中每个捆绑都有一个优先级, 优先级的规模是1 ~ 1000。创建一个捆绑,默许的优先级是最高的1000;Content Hugging Priority: 该优先级标明一个控件抗被拉伸的优先级。优先级越高,越不容易被拉伸,默许是250(UILayoutPriorityDefaultLow)。
Content Compression Resistance Priority: 该优先级和上面那个优先级相对应,标明一个控件抗紧缩的优先级。优先级越高,越不容易被紧缩,默许是750(UILayoutPriorityDefaultHigh);
通过以下两个方法来设置他们的抗被拉伸的优先级和抗紧缩的优先级:
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis API_AVAILABLE(ios(6.0));
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis;
优先级取值:
static const UILayoutPriority UILayoutPriorityRequired = 1000;
static const UILayoutPriority UILayoutPriorityDefaultHigh = 750;
static const UILayoutPriority UILayoutPrioritySceneSizeStayPut = 500;
static const UILayoutPriority UILayoutPriorityDefaultLow = 250;
static const UILayoutPriority UILayoutPriorityFittingSizeLevel = 50;
场景一
- (void)test {
UILabel *leftLabel = [UILabel new];
leftLabel.text = @"我是左边我是我是左边";
leftLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:leftLabel];
UILabel *rightLabel = [UILabel new];
rightLabel.text = @"议论数:20";
rightLabel.backgroundColor = [UIColor yellowColor];
[self.view addSubview:rightLabel];
[leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.left.mas_equalTo(10);
}];
[rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(leftLabel.mas_right).offset(10);
make.right.mas_equalTo(-10);
make.top.equalTo(leftLabel.mas_top);
}];
}
默许闪现效果如图
假设希望左边的自适应,右边的自动填充,那么咱们需要增大左边的抗被拉伸优先级(越大越不容易被拉伸,默许250、UILayoutPriorityDefaultLow),或许减小右边的抗被拉伸优先级,这样才能让右边的label内容优先闪现。
添加以下代码设置:
[leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
或许
[rightLabel setContentHuggingPriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
闪现效果
在添加上述的代码往后假设添加左边的文字,效果如下:
此时发现右边的文字无法闪现了,持续向下剖析;
场景二
- (void)test {
UILabel *leftLabel = [UILabel new];
leftLabel.text = @"我是左边我是左边我是左边我是左边我是左边我是左边我是左边我是左边我是左边";
leftLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:leftLabel];
UILabel *rightLabel = [UILabel new];
rightLabel.text = @"议论数:20";
rightLabel.backgroundColor = [UIColor yellowColor];
[self.view addSubview:rightLabel];
[leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.left.mas_equalTo(10);
}];
[rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(leftLabel.mas_right).offset(10);
make.right.mas_equalTo(-10);
make.top.equalTo(leftLabel.mas_top);
}];
}
左边内容多超越屏幕,默许闪现效果如图
假设希望右边完全闪现,左边内容自适应,那么咱们需要增大右边的抗紧缩优先级(优先级越高,越不容易被紧缩,默许750、UILayoutPriorityDefaultHigh),或许减少左边的抗紧缩优先级;
添加以下代码设置:
[leftLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
或许
[rightLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]
闪现效果
在添加上述的代码往后假设,减少左边的文字,效果如下:
场景三
需求:
-
黄色内容完全展示而且在赤色右边,而且距离一向为10
-
赤色内容少的时分自适应、内容多的时分闪现…
解决方案:
这个时分需要将场景一和场景2的内容一起运用,也便是一起运用抗紧缩和抗拉伸优先级:
[leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[rightLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]
或许
[rightLabel setContentHuggingPriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
[leftLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
即场景一和场景二中的代码顽固其一进行组合,这儿只列出了2种;
工作效果1(左边内容多的时分):
工作效果2(左边内容少的时分):
总结
-
空间足够的情况下,能够设置某个控件的抗被拉伸优先级,让某个控件或其他控件占用剩下空间;
-
空间缺少的情况下,能够设置某个控件的抗紧缩优先级,让某个控件展示完全;