Masonry框架的基本使用

//

//  ViewController.m

//  massory框架的使用

//

//  Created by ZXC5245 on 16/4/18.

//  Copyright © 2016 湛孝超. All rights reserved.

//

#import <Masonry/Masonry.h>

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    // masonry 添加约束

    /*

     注意:想清楚参考系就行了 谁对谁添加约束

     */

    // 1 先创建对象

    UIView *redView = [[UIView alloc] init];

    redView.backgroundColor = [UIColor redColor];

#warning 在添加约束之前 必须先把 控件添加到视图上 再添加控件的约束

    // 2 将创建好的控件添加到视图上

    [self.view addSubview:redView];

    // 3 给控件再添加约束

    [redView mas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.center.equalTo(self.view);

        make.size.mas_equalTo(CGSizeMake(300, 300));

    }];

//    // 4 重设约束

    [redView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view).offset(10);

        // 注意设置约束的参考系是他的父 View

        make.right.equalTo(self.view).offset(-10);

        // 设置相对于父view的顶部偏移20

        make.top.equalTo(self.view).offset(20);

        //  设置 此时view的高度是 100

        make.height.mas_equalTo(100);

        

    }];

//    // 5 更新约束 此时只改变了红色view 大小值 依然是center 对齐

//    [redView mas_updateConstraints:^(MASConstraintMaker *make) {

//        make.size.mas_equalTo(CGSizeMake(400, 400));

//    }];

#pragma mark -- 添加一个蓝色view 来根据红色view 来设置约束

    UIView *blueView = [[UIView alloc] init];

    blueView.backgroundColor = [UIColor blueColor];

    [self.view addSubview:blueView];

    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {

        //设置蓝色view相当于红色view的底部偏移10

        make.top.equalTo(redView.mas_bottom).offset(10);

        //设置蓝色View相对于红色View左对齐

        make.leading.equalTo(redView);

        //设置蓝色view的宽度约束是红色view1/2

        make.width.equalTo(redView.mas_width).multipliedBy(0.5);

        //设置蓝色view的高度约束等于红色view

        make.height.mas_equalTo(redView.mas_height);

    }];


}

@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值