AutoLayout之Content Hugging Priority和Content Compression Resistance Priority

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章