UIiOS面试

iOS Autolayout中 intrinsic conten

2018-11-15  本文已影响14人  炎成

如果要搞明白约束优先级的问题,那就绕不开 intrinsic content size 的概念。

了解了以上几个概念之后我们来看实践中所遇到的问题。

UILabel *labelLeft = [UILabel new];
    labelLeft.backgroundColor =  [UIColor yellowColor];
    labelLeft.text = @"我最喜欢的金庸先生的《笑傲江湖》"; 
//    [labelLeft setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    [self.view addSubview:labelLeft];
    
    UILabel *labelRight = [UILabel new];
    labelRight.backgroundColor =  [UIColor blueColor];
    labelRight.text = @"这是一段较短的文字";
//    [labelRight setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.view addSubview:labelRight];
    
    [labelLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.lessThanOrEqualTo(labelRight.mas_left).offset(-5);
        make.top.offset(100);
    }];
    [labelRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.greaterThanOrEqualTo(labelLeft.mas_right).offset(5);
        make.right.offset(-10);
        make.top.offset(100);
    }];

在不单独设置Content Compression Resistance Priority,采用默认优先级时下


image.png

布局会按照从左到右的顺序先将左侧label显示完毕,右侧宽度不够于是省略。
如果有需求需要右侧文本显示完毕,此时就可以通过设置Content Compression Resistance Priority的优先级来处理。两种方案,只要设置labelLeft和labelRight的优先级不同即可:

//方法一降低labelLeft的抗压缩优先级
    [labelLeft setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
//方法二升高labelRight的抗压缩优先级
//    [labelRight setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

执行效果相同。


image.png

接下来讲一下Content Hugging Priority使用场景。

[labelLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
//        make.right.lessThanOrEqualTo(labelRight.mas_left).offset(-5);
        make.right.equalTo(labelRight.mas_left).offset(-5);
        make.top.offset(100);
    }];
    [labelRight mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.greaterThanOrEqualTo(labelLeft.mas_right).offset(5);
        make.left.equalTo(labelLeft.mas_right).offset(5);
        make.right.offset(-10);
        make.top.offset(100);
    }];
在采用默认的Content Hugging Priority和Content Compression Resistance Priority时,结果如下 image.png

左侧label被拉伸,但是如果我们需要的是拉伸右侧,则可以设置

    //升高labelLeft的抗拉伸优先级
    [labelLeft setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
image.png

再来看一下Storyboard中拖入的控件与代码创建控件的优先级问题


image.png

可以看到Content Hugging Priority默认为251

    UILayoutPriority priority = [self.storyboardLabel contentHuggingPriorityForAxis:UILayoutConstraintAxisHorizontal];  //251
    UILayoutPriority rightLabelPriority = [labelRight contentHuggingPriorityForAxis:UILayoutConstraintAxisHorizontal];  //250
    NSLog(@"storyboard contentHuggingPriority = %f \n allocLabel contentHuggingPriority = %f",priority,rightLabelPriority);

[labelRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.storyboardLabel.mas_right).offset(5);
        make.right.offset(-10);
        make.centerY.equalTo(self.storyboardLabel);
    }];

如上,在都使用默认Content Hugging Priority时,storyboard创建的label优先级高于代码创建label,结果如下


image.png
//若设置labelRight优先级252,则会拉伸左侧label
[labelRight setContentHuggingPriority:252 forAxis:UILayoutConstraintAxisHorizontal];

image.png

参考文章:https://www.jianshu.com/p/f83fa37fdd46

上一篇下一篇

猜你喜欢

热点阅读