Masonry自适应屏幕

 1 #import "RootViewController.h"
 2 #import "Masonry.h"
 3 @interface RootViewController ()
 4 
 5 @end
 6 
 7 @implementation RootViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     
12 #pragma mark lable
13     // 添加约束,不需要设置frame
14     UILabel *lable = [UILabel new];
15     lable.backgroundColor = [UIColor redColor];
16     // 添加到父视图, 视图添加完成后才能进行布局
17     [self.view addSubview:lable];
18     // 布局, 实现布局方法
19     [lable mas_makeConstraints:^(MASConstraintMaker *make) {
20        
21         // 距离上面50 (参照系统的可以不写)自定义的要写-----括号里面
22         // make: 相当于你要布局的视图
23         // equalTo(参照视图对象), 如果参照视图是self.view.可以不设置参照视图的属性
24         // offset(距离数值)
25         make.top.equalTo(self.view).offset(50);
26         // 距离左边100
27         make.left.equalTo(self.view).offset(100);
28         // 距离右边100
29         make.right.equalTo(self.view).offset(-100);
30         // 距离下边500
31         make.bottom.equalTo(self.view).offset(-500);
32     
33         
34     }];
35     
36 #pragma mark lable1
37     
38     UILabel *lable1 = [UILabel new];
39     lable1.backgroundColor = [UIColor greenColor];
40     [self.view addSubview:lable1];
41     // 先布局参照视图, 否则约束容易丢失
42     [lable1 mas_makeConstraints:^(MASConstraintMaker *make) {
43         // 布局lable1 和lable左边一致
44         // equalTo (自定义视图), 需要设置视图属性
45         // 如果数值为0,可以不写offset()
46         make.leading.equalTo(lable.mas_leading);
47         
48          // 布局lable1 和lable右边一致
49         make.trailing.equalTo(lable.mas_trailing);
50         // 上边距离Lable1 50
51         make.top.equalTo(lable.mas_bottom).offset(50);
52 
53         // 高度60
54         make.height.mas_equalTo(60);
55     }];
56 #pragma mark lable2
57     
58     UILabel *lable2 = [UILabel new];
59     lable2.backgroundColor = [UIColor cyanColor];
60     [self.view addSubview:lable2];
61     // 结构体(内边距)
62     // 设置距离参照视图的内边距(上左下右)
63     UIEdgeInsets pading = UIEdgeInsetsMake(400, 100, 100, 100);
64     
65     [lable2 mas_makeConstraints:^(MASConstraintMaker *make) {
66     
67 //        make.leading.equalTo(self.view).offset(100);
68 //        make.trailing.equalTo(self.view).offset(-100);
69 //        make.bottom.equalTo(self.view).offset(-100);
70 //        make.top.equalTo(self.view).offset(400);
71         // 设置约束视图的边界距离self.view的边界值
72         make.edges.equalTo(self.view).insets(pading);
73         
74     
75     }];
76     
77 #pragma mark lable3
78     
79     UILabel *lable3 = [UILabel new];
80     lable3.backgroundColor = [UIColor orangeColor];
81     [self.view addSubview:lable3];
82     [lable3 mas_makeConstraints:^(MASConstraintMaker *make) {
83        
84         // 设置中心点一致
85         make.center.equalTo(lable2);
86         // 设置大小
87         // make.width = lable2.width - 40
88         // make.height = labele2.height - 60
89         make.size.equalTo(lable2).sizeOffset(CGSizeMake(-40, -60));
90     }];
91 }
原文地址:https://www.cnblogs.com/leikun1113/p/5544280.html