Masonry

视图中如果还有视图 使用cgrectMake计算坐标的时候 会很麻烦于是 找到了masonry 非常好用 下面写一些简单的示范

首先下载一个masonry 在viewController .h文件中引入Masonry,h文件即可


 - (void)viewDidLoad  {

 

self.view.backgroundColor = [UIColor whiteColor];

    

   

    

    

    UIView *sv = [UIView new];

    sv.backgroundColor = [UIColor redColor];

    [self.view addSubview:sv];

    

    [sv mas_makeConstraints:^(MASConstraintMaker *make) {

        make.center.equalTo(self.view);

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

    }];

    

    UIView *sv1 = [UIView new];

    [sv addSubview:sv1];

    sv1.backgroundColor = [UIColor yellowColor];

    [sv1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

        /* 等价于

        make.top.equalTo(sv).with.offset(10);

        make.left.equalTo(sv).with.offset(10);

        make.bottom.equalTo(sv).with.offset(-10);

        make.right.equalTo(sv).with.offset(-10);

        */

        /* 也等价于

        make.top.left.bottom.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

        */

    }];

    

    int padding1 = 10;

    UIView *sv2 = [UIView new];

    sv2.backgroundColor = [UIColor blueColor];

    

    UIView *sv3 = [UIView new];

    sv3.backgroundColor = [UIColor blueColor];

    

    [self.view addSubview:sv2];

    [self.view addSubview:sv3];

    

    [sv2 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(sv.mas_centerY);

        make.left.equalTo(sv.mas_left).with.offset(padding1);

        make.right.equalTo(sv3.mas_left).with.offset(-padding1);

        make.height.mas_equalTo(@150);

        make.width.equalTo(sv3);

    }];

    

    

    [sv3 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(sv.mas_centerY);

        make.left.equalTo(sv2.mas_right).with.offset(padding1);

        make.right.equalTo(sv.mas_right).with.offset(-padding1);

        make.height.mas_equalTo(@150);

        make.width.equalTo(sv2);

    }];


       

 

    

    // 让两个高度为150 view垂直居中且等宽等间隔排列 间隔为10(自动计算宽度)

    

    self.view.backgroundColor = [UIColor orangeColor];

    UIView *sv = [UIView new];

    sv.backgroundColor = [UIColor redColor];

    [self.view addSubview:sv];

    

    // Masonryautolauout添加函数 将所需要的约束添加到block

    [sv mas_makeConstraints:^(MASConstraintMaker *make) {

        // 居中

        make.center.equalTo(self.view);

        // 设置宽高

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

    }];

    

    UIScrollView *scrollView = [UIScrollView new];

    scrollView.backgroundColor = [UIColor whiteColor];

    [sv addSubview:scrollView];

    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(5, 5, 5, 5));

    }];

    UIView *container = [UIView new];

    [scrollView addSubview:container];

    [container mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(scrollView);

        make.width.equalTo(scrollView);

    }];

    int count = 10;

    UIView *lastView = nil;

    for (int i = 1; i <= count; ++i) {

        UIView *subView = [UIView new];

        [container addSubview:subView];

        subView.backgroundColor = [UIColor colorWithHue:arc4random() % 256 / 256.0 saturation:arc4random() %128 / 256.0 + 0.5 brightness:arc4random() % 128 / 256.0 + 0.5 alpha:1];

        [subView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.and.right.equalTo(container);

            make.height.mas_equalTo(@(20 * i));

            

            if (lastView) {

                make.top.mas_equalTo(lastView.mas_bottom);

            } else {

                make.top.mas_equalTo(container.mas_top);

            }

            

        }];

        lastView = subView;

    }

    

    [container mas_makeConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(lastView.mas_bottom);

    }];


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值