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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值