Masonry 优先级(简单明了)

一、AutoLayout有两个重要的属性:

1.Content Compression Resistance 百度翻译(内容压缩抗力)
2.Content Hugging 百度翻译(内容拥抱)

二、解释

Content Compression Resistance = 变大!(保持自己更大)

这个属性的优先级(Priority)越高,越不“容易”被压缩。也就是说,当整体的空间装不下所有的View的时候,Content Compression Resistance优先级越高的,现实的内容越完整。

[self.labelTwo setContentCompressionResistancePriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
Content Hugging = 抱紧!(保持自己更小)

这个属性的优先级越高,整个View就要越“抱紧”View里面的内容。也就是View的大小不会随着父级View的扩大而扩大。

[self.labelOne setContentHuggingPriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
 >参数一:(UILayoutPriority)
 >设置优先级等级,数值越大,优先级越高。
 >UILayoutPriorityRequired           == 1000;
 >UILayoutPriorityDefaultHigh        == 750;
 >UILayoutPriorityDefaultLow         == 250;
 >UILayoutPriorityFittingSizeLevel   == 50;


>参数二:(UILayoutConstraintAxis)
>百度翻译(Axis:轴线。意思是你添加的优先级是Horizontal还是Vertical)
 >UILayoutConstraintAxisHorizontal
 >UILayoutConstraintAxisVertical

三、代码部分

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()

@property(nonatomic,strong) UILabel * labelOne;
@property(nonatomic,strong) UILabel * labelTwo;

@end

@implementation ViewController


- (void)initUI
{
    [self.view addSubview:self.labelOne];
    [self.labelOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(20);
        make.top.equalTo(self.view.mas_top).with.offset(20);
    }];
    
    [self.view addSubview:self.labelTwo];
    [self.labelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_labelOne.mas_right);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).with.offset(20);
        make.height.equalTo(_labelOne);
    }];
    
    [self.labelOne setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    /**参数一:(UILayoutPriority)
     UILayoutPriorityRequired
     UILayoutPriorityDefaultHigh
     UILayoutPriorityDefaultLow
     UILayoutPriorityFittingSizeLevel
     */
    
    /**参数二:(UILayoutConstraintAxis)
     UILayoutConstraintAxisHorizontal
     UILayoutConstraintAxisVertical
     */
    [self.labelTwo setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

- (UILabel *)labelOne{
    if (!_labelOne) {
        self.labelOne = [[UILabel alloc] init];
        self.labelOne.text = @"这是labelOne";
        self.labelOne.backgroundColor = [UIColor redColor];
    }
    return _labelOne;
}

- (UILabel *)labelTwo{
    if (!_labelTwo) {
        self.labelTwo = [[UILabel alloc] init];
        self.labelTwo.text = @"这是labelTwo";
        self.labelTwo.backgroundColor = [UIColor greenColor];
    }
    return _labelTwo;
}
@end

尊重原创

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,392评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,828评论 0 1
  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 1,195评论 1 4
  • AutoLayout框架Masonry使用心得 我们组分享会上分享了页面布局的一些写法,中途提到了AutoLayo...
    番茄炒西红柿啊阅读 1,187评论 0 4
  • AutoLayout AutoLayout是基于约束的描述性的布局系统 官方文档: https://develop...
    cooooooop阅读 699评论 0 1