美文网首页源码解读
Masonry 优先级(简单明了)

Masonry 优先级(简单明了)

作者: 一个脱离了高级趣味的人e | 来源:发表于2018-07-10 14:54 被阅读243次

    一、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 优先级(简单明了)

        本文链接:https://www.haomeiwen.com/subject/aoyfpftx.html