iOS-布局-Masonry-优先级

一.AutoLayout有两个重要的属性

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

2.Content Hugging    百度翻译(内容拥抱) 

  • Content Compression Resistance = 变大!(保持自己更大)
这个属性的优先级(Priority)越高,越不“容易”被压缩。也就是说,当整体的空间装不下所有的View的时候,Content Compression Resistance优先级越高的,现实的内容越完整。
 
[self.labelTwosetContentCompressionResistancePriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
 
  • Content Hugging = 抱紧!(保持自己更小)
这个属性的优先级越高,整个View就要越“抱紧”View里面的内容。也就是View的大小不会随着父级View的扩大而扩大。
[self.labelOnesetContentHuggingPriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
 
    /**参数一:(UILayoutPriority)
      设置优先级等级,数值越大,优先级越高。
     UILayoutPriorityRequired           == 1000;
     UILayoutPriorityDefaultHigh        == 750;
     UILayoutPriorityDefaultLow         == 250;
     UILayoutPriorityFittingSizeLevel   == 50;
     */
   
    /**参数二:(UILayoutConstraintAxis)
       百度翻译(Axis:轴线。意思是你添加的优先级是Horizontal还是Vertical)
     UILayoutConstraintAxisHorizontal
     UILayoutConstraintAxisVertical
     */
二.代码部分
使用懒加载添加控件:详情请看本博客懒加载。
 1 #import "ViewController.h"
 2 #import "Masonry.h"
 3 
 4 @interface ViewController ()
 5 
 6 @property(nonatomic,strong) UILabel * labelOne;
 7 @property(nonatomic,strong) UILabel * labelTwo;
 8 
 9 @end
10 
11 @implementation ViewController
12 
13 
14 - (void)initUI
15 {
16     [self.view addSubview:self.labelOne];
17     [self.labelOne mas_makeConstraints:^(MASConstraintMaker *make) {
18         make.left.equalTo(self.view.mas_left).with.offset(20);
19         make.top.equalTo(self.view.mas_top).with.offset(20);
20     }];
21     
22     [self.view addSubview:self.labelTwo];
23     [self.labelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
24         make.left.equalTo(_labelOne.mas_right);
25         make.right.equalTo(self.view.mas_right);
26         make.top.equalTo(self.view.mas_top).with.offset(20);
27         make.height.equalTo(_labelOne);
28     }];
29     
30     [self.labelOne setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
31     /**参数一:(UILayoutPriority)
32      UILayoutPriorityRequired
33      UILayoutPriorityDefaultHigh
34      UILayoutPriorityDefaultLow
35      UILayoutPriorityFittingSizeLevel
36      */
37     
38     /**参数二:(UILayoutConstraintAxis)
39      UILayoutConstraintAxisHorizontal
40      UILayoutConstraintAxisVertical
41      */
42     [self.labelTwo setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
43 }
44 - (void)viewDidLoad {
45     [super viewDidLoad];
46 
47     [self initUI];
48 }
49 
50 - (UILabel *)labelOne
51 {
52     if (!_labelOne) {
53         self.labelOne = [[UILabel alloc] init];
54         self.labelOne.text = @"这是labelOne";
55         self.labelOne.backgroundColor = [UIColor redColor];
56     }
57     return _labelOne;
58 }
59 
60 - (UILabel *)labelTwo
61 {
62     if (!_labelTwo) {
63         self.labelTwo = [[UILabel alloc] init];
64         self.labelTwo.text = @"这是labelTwo";
65         self.labelTwo.backgroundColor = [UIColor greenColor];
66     }
67     return _labelTwo;
68 }
69 
70 @end
你的一次推荐就是对我莫大的支持。感觉不错,给个推荐或者评论吧。
原文地址:https://www.cnblogs.com/mancong/p/5057071.html