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

2018-06-26  本文已影响113人  zhengchengxia

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;

    }

}

上一篇下一篇

猜你喜欢

热点阅读