您的位置:首页 > 其它

Masonry实现九宫格

2015-12-03 10:26 288 查看
效果:



Code:

/**

 *  @author Etz

 *

 *  创建九宫格

 */

- (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++;

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息