masonry的使用介绍

#import "ViewController.h"

#import "Masonry.h"


@interface ViewController ()


@property (nonatomic, strong) UIView *weakSelfView;

@property (nonatomic, strong) UIView *view1;

@property (nonatomic, strong) UIView *view2;

@property (nonatomic, strong) UIView *view3;

@property (nonatomic, strong) UIView *view4;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    //block里面要用self的若引用 避免循环引用

    _weakSelfView = self.view;

    

    [self createView1];

    

    [self createView2];

    

    [self createView3];

    

    [self createView4];

    

    

    

/***************有关约束的三个方法

 

 添加约束

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;

 更新约束

- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;

 移除所有约束

- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

 

 ****************/

}


#pragma mark 单个属性设置view1

/****************单个属性设置     【注意】多个约束的时候不要出现约束冲突

 @property (nonatomic, strong, readonly) MASConstraint *left;       //居左

 @property (nonatomic, strong, readonly) MASConstraint *top;        //居上

 @property (nonatomic, strong, readonly) MASConstraint *right;      //居右

 @property (nonatomic, strong, readonly) MASConstraint *bottom;     //局下

 @property (nonatomic, strong, readonly) MASConstraint *leading;    //首部

 @property (nonatomic, strong, readonly) MASConstraint *trailing;   //尾部

 @property (nonatomic, strong, readonly) MASConstraint *width;      //

 @property (nonatomic, strong, readonly) MASConstraint *height;     //

 @property (nonatomic, strong, readonly) MASConstraint *centerX;    //中心x

 @property (nonatomic, strong, readonly) MASConstraint *centerY;    //中心y

 @property (nonatomic, strong, readonly) MASConstraint *baseline;   //文本基线

 

 上、左,正数表示在里面

 下、右,负数表示在里面

 ***********************/

-(void)createView1

{

    _view1 = [[UIView alloc] init];

    _view1.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:self.view1];

    

    //self.view1添加约束

    [self.view1 mas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.top.equalTo(_weakSelfView.mas_top).with.offset(64);         //居上

        make.right.equalTo(_weakSelfView.mas_right).with.offset(-10);    //居右

        make.left.equalTo(_weakSelfView.mas_left).with.offset(10);       //居左

        //        make.bottom.equalTo(weakSelfView.mas_bottom).with.offset(-200); //局下

        make.height.equalTo(@200);                                      //高度

        

    }];

}


#pragma mark 设置整体边界

//make.edges.equalTo(_view1).with.insets(edge);

//表示相对于父视图view1的边界,边界都是在父视图里面,不需要带负数

-(void)createView2

{

    _view2 = [[UIView alloc] init];

    _view2.backgroundColor = [UIColor blackColor];

    [self.view addSubview:self.view2];

    

    //这四个值分别对应  居上、居左、局下、居右(最上面开始,逆时针方向)

    UIEdgeInsets edge = UIEdgeInsetsMake(100, 30, 50, 30);

    [self.view2 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(_view1).with.insets(edge);

    }];

    

}


#pragma mark 使用CGPointMakeCGSizeMake设置属性

//make.center.mas_equalTo(CGPointMake(0, 0));

//中心坐标是相对于父视图的中心坐标

-(void)createView3

{

    _view3 = [[UIView alloc] init];

    _view3.backgroundColor = [UIColor greenColor];

    [self.view addSubview:self.view3];

    

    [self.view3 mas_makeConstraints:^(MASConstraintMaker *make) {

    

        make.center.mas_equalTo(CGPointMake(0, 0));             //中心坐标

        make.size.mas_equalTo(CGSizeMake(100, 100));            //大小

        

    }];

    

}


#pragma mark 同时设置多个属性

//就是和我们用xib拖空间时设置的约束是一样的,仅仅是这个要用代码表示而已

-(void)createView4

{

    _view4 = [[UIView alloc] init];

    _view4.backgroundColor = [UIColor grayColor];

    [self.view addSubview:self.view4];

    

    [self.view4 mas_makeConstraints:^(MASConstraintMaker *make) {

       

        make.bottom.equalTo(_weakSelfView.mas_bottom).with.offset(-20);

        make.top.equalTo(self.view3.mas_top).with.offset(200);

        make.left.and.right.equalTo(self.view2);

//        make.width.equalTo(self.view2.mas_width).multipliedBy(1.0);

        

    }];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值