masonry 约束布局


之前我们在屏幕适配的章节中学习过 AutoLayout 的使用,但那都是在可视化界面上进行添加约束完成的,我们很多时候都需要在代码中使用 AutoLayout 约束,苹果也为我们提供了实现,使用 NSLayoutConstraint 类表示约束,但使用起来比较复杂,代码量比较大 ,Masonry 是一个轻量级的布局框架,它拥有自己的描述语法(采用更优雅的链式语法封装)来自动布局,具有很好可读性且同时支持 iOS和Max OS X 等。总之,对于侧重写代码的coder,请你慢慢忘记 Frame ,喜欢 Masonry 吧。下面我就登录框做一个小例子。使用前需要导入Masonry 框架 使用pods 直接拖进工程都行都行,

直接进入Github搜索Masonry进行源码下载使用CocoaPod进行下载


#import "Masonry.h"


UILabel *label = [[UILabel alloc] init];
    label.text = @"用户名:";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor greenColor];
    label.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:label];
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"密 码:";
    label1.textAlignment = NSTextAlignmentCenter;
    label1.textColor = [UIColor blackColor];
    label1.backgroundColor = [UIColor greenColor];
    label1.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:label1];
    
    UITextField *tf = [[UITextField alloc] init];
    tf.backgroundColor = [UIColor greenColor];
    tf.placeholder = @"请输入用户名";
    tf.borderStyle = UITextBorderStyleRoundedRect;
    
    [self.view addSubview:tf];
    
    UITextField *tf1 = [[UITextField alloc] init];
    tf1.backgroundColor = [UIColor greenColor];
    tf1.placeholder = @"请输入密码";
    tf1.secureTextEntry = YES;
    tf1.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:tf1];
    
    UIButton *but =[UIButton buttonWithType:UIButtonTypeCustom];
    [but setTitle:@"登录" forState:UIControlStateNormal];
    [but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [but setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:but];
    UIButton *but1 =[UIButton buttonWithType:UIButtonTypeCustom];
    [but1 setTitle:@"注册" forState:UIControlStateNormal];
    [but1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [but1 setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:but1];
    
    
    __weak typeof(self) vc = self;
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vc.view).offset(100);
        make.left.equalTo(vc.view).offset(50);
        make.height.equalTo([NSNumber numberWithInt:30]);
        make.width.equalTo([NSNumber numberWithInt:60]);
        
    }];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(5);
        make.left.equalTo(label);
        make.height.equalTo(label);
        make.width.equalTo(label);
    }];
    
    [tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.height.equalTo(label);
        make.left.equalTo(label.mas_right).offset(10);
        make.right.equalTo(vc.view).offset(-50);
        
    }];
    
    [tf1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.height.equalTo(label1);
        make.left.equalTo(label1.mas_right).offset(10);
        make.right.equalTo(vc.view).offset(-50);
        
    }];
    
    [but mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(tf1.mas_bottom).offset(10);
        make.left.equalTo(label1);
        make.height.equalTo(label1);
        make.right.equalTo(tf1);
        
    }];
    [but1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(but.mas_bottom).offset(10);
        make.left.equalTo(but);
        make.height.equalTo(but);
        make.right.equalTo(but);
    }];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值