iOS-佈局-Masonry-優先級

一.AutoLayout有兩個重要的屬性

1.Content Compression Resistance 百度翻譯(內容壓縮抗力)ui

2.Content Hugging    百度翻譯(內容擁抱) atom

  • 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
相關文章
相關標籤/搜索