Masonry第三方布局九宫格的使用随笔

Masonry是个好东西,在当前尺寸各异的iOS开发适配中发挥着至关重要的作用,由于项目中Masonry布局用的比较多,对于UI布局也有了一些自己的理解,经常会有人问道Masonry布局九宫格要怎么布局呢,单行、多行要怎么做到自动布局呢,之前用frame布局九宫格需要2层for循环,各种判断才可以完成一套九宫格布局,那使用Masonry是不是也这么麻烦呢,答案是否定的!下面把Masonry布局单行,多行的代码贴出来,注释的很详细,有需要的同学可以参考参考,可能对于Masonry的使用会有不一样的理解。

图片

- (void)viewDidLoad {

    [super viewDidLoad];

    [self daohang];

    self.view.backgroundColor = [UIColor blackColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake((self.view.frame.size.width - 100)/2, 64, 100, 50);

    [button setTitle:@"wedding" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor brownColor];

    [button addTarget:self action:@selector(playMovie) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];


//    int padding = 10;

//    UIView *superview = self.view;


    UIButton *hxButton = [UIButton buttonWithType:UIButtonTypeCustom];

    hxButton.backgroundColor = [UIColor redColor];

    [hxButton setTitle:@"涵予" forState:UIControlStateNormal];

    [hxButton addTarget:self action:@selector(hxBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:hxButton];

    [hxButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_greaterThanOrEqualTo(button.mas_top).offset(100);

        make.left.mas_equalTo(button.mas_left).offset(0);

        //        make.bottom.mas_equalTo(superview.mas_bottom).offset(-padding);

        //        make.right.mas_equalTo(superview.mas_right).offset(-padding);

        make.width.equalTo(button.mas_width);


        make.width.mas_equalTo(100);

        make.height.mas_equalTo(50);

        make.height.offset(50);


    }];


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

    [self.view addSubview:containView];

    containView.backgroundColor = [UIColor yellowColor];

    [containView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(15);

        make.right.mas_equalTo(-15);

        make.top.mas_equalTo(260);

        make.height.equalTo(@300);

    }];



    CGFloat marginX = 10;  //按钮距离左边和右边的距离

    CGFloat marginY = 1;  //距离上下边缘距离

//    CGFloat toTop = 200;  //按钮距离顶部的距离

    CGFloat gapX = 10;    //左右按钮之间的距离

    CGFloat gapY = 10;    //上下按钮之间的距离

    NSInteger col = 3;    //这里只布局5列

//    NSInteger count = 13;  //这里先设置布局任意个按钮

    CGFloat width = (self.view.frame.size.width - 70) / 3;


    UIButton *last = nil;

    for (int i = 0; i < 7; i++) {

        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

        [containView addSubview:button1];

        if(i % 2 == 0) {

            button1.backgroundColor = [UIColor redColor];

        } else {

            button1.backgroundColor = [UIColor darkGrayColor];

        }

        [button1 mas_makeConstraints:^(MASConstraintMaker *make) {

            make.width.mas_equalTo(width);

            make.height.mas_equalTo(50);

            CGFloat top = 10 + marginY + (i / col) * (50 + gapY);

            make.top.mas_offset(top);

            if(!last || (i%col) == 0) {

                make.left.mas_offset(marginX);

            } else {

                make.left.mas_equalTo(last.mas_right).mas_offset(gapX);

            }

        }];

        last = button1;

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 4,954评论 1 4
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 5,817评论 0 1
  • 一、前言 关于苹果的布局一直是我比较纠结的问题,是写代码来控制布局,还是使用storyboard来控制布局呢?以前...
    iplaycodex阅读 7,243评论 0 1
  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 7,079评论 0 3
  • Masonry是一个轻量级的布局框架,它拥有自己的描述语法(采用更优雅的链式语法封装)来自动布局,具有很好可读性且...
    AngeloD阅读 8,632评论 0 9