天天看点

Masonry实现九宫格

效果:

Masonry实现九宫格

Code:

- (void) createJiugongge {

    int padding = 10;

    float width = (CGRectGetWidth(self.view.bounds) - 40) / 3;

    int count = 1;

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

        for(int j = 0; j < 3; j++) {

            UIView *view = [UIView new];

            view.backgroundColor = [UIColor redColor];

            [self.view addSubview:view];

            [view mas_makeConstraints:^(MASConstraintMaker *make) {

                make.left.mas_equalTo(self.view).offset(padding+(padding+width)*j);

                make.top.mas_equalTo(self.view).offset(74+(padding+width)*i);

                make.width.and.height.mas_equalTo(width);

            }];

            UILabel *l = [UILabel new];

            l.text = [NSString stringWithFormat:@"%d", count];

            l.textAlignment = NSTextAlignmentCenter;

            l.font = [UIFont systemFontOfSize:50];

            l.textColor = [UIColor whiteColor];

            [self.view addSubview:l];

            [l mas_makeConstraints:^(MASConstraintMaker *make) {

                make.center.mas_equalTo(view);

                make.size.mas_equalTo(view);

            }];

            count++;

        }

    }

}

继续阅读