1、首先要引入一个概念intrinsic size,即视图本质的尺寸,它会随着内容的不同而不同,它由intrinsicContentSize返回。
intrinsicContentSize
通过我测试和理解,我对intrinsicContentSize的理解是如果一个控件拥有intrinsic size,则它的返回尺寸根据内容而决定;否则,返回的尺寸是(-1,-1)。例如,label拥有intrinsic size,则当label.text = @"12432423"时intrinsicContentSize返回的值肯定比label.text=@"1"返回的值大。
2、Content Hugging Priority和Content Compression Resistance Priority
Content Hugging:内容压缩,阻止视图的实际尺寸比intrinsicContentSize返回的更大
Content Compression Resistance:阻止内容压缩,阻止视图的实际尺寸比intrinsicContentSize返回的更小。
Content Hugging Priority:内容压缩优先级,默认为250
Content Compression Resistance Priority:阻止内容压缩优先级,默认为750
3、代码测试
- (UILabel *)labelWithSuperView:(UIView *)superView topView:(UIView *)topView text:(NSString *)text{
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor lightGrayColor];
label.text = text;
label.translatesAutoresizingMaskIntoConstraints = NO;
[superView addSubview:label];
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:50];
[superView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:topView attribute:NSLayoutAttributeTop multiplier:1.0 constant:100];
[superView addConstraint:constraint];
return label;
}
- (void)constraintWidthWithLabel:(UILabel *)label priority:(UILayoutPriority)priority width:(CGFloat)width{
NSString *formatStr = [NSString stringWithFormat:@"[label(%f)]", width];
NSArray *constraintArr = [NSLayoutConstraint constraintsWithVisualFormat:formatStr options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(label)];
for(NSLayoutConstraint *constraint in constraintArr){
constraint.priority = priority;
}
[self.view addConstraints:constraintArr];
}
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label1 = [self labelWithSuperView:self.view topView:self.view text:@"label1 hello"];
UILabel *label2 = [self labelWithSuperView:self.view topView:label1 text:@"label2 hello"];
[self constraintWidthWithLabel:label2 priority:751 width:200];
UILabel *label3 = [self labelWithSuperView:self.view topView:label2 text:@"label3 hello"];
[self constraintWidthWithLabel:label3 priority:200 width:200];
UILabel *label4 = [self labelWithSuperView:self.view topView:label3 text:@"label4 hello"];
[self constraintWidthWithLabel:label4 priority:751 width:70];
UILabel *label5 = [self labelWithSuperView:self.view topView:label4 text:@"label5 hello"];
[self constraintWidthWithLabel:label5 priority:749 width:70];
// Do any additional setup after loading the view, typically from a nib.
}
代码解释:
label1不约束宽度
label2约束宽度为200,约束的优先级为751,大于Content Hugging Priority(250)
label3约束宽度为200,约束的优先级为200,小于Content Hugging Priority(250)
label4约束宽度为60, 约束的优先级为751,大于Content Compression Resistance Priority(750)
label5约束宽度为60, 约束的优先级为749,小于Content Compression Resistance Priority(750)
效果如下:
参考:http://blog.csdn.net/yongyinmg/article/details/39526207