masonry 九宮格

  1. 固定寬高,以及間距的情況
- (void)confUI
{
    CGFloat w = (ScreenWidth -24)/3;
    UIView *bgview = [[UIView alloc]init];
    [self addSubview:bgview];
    bgview.layer.borderColor = [UIColor blackColor].CGColor;
    bgview.layer.borderWidth = 1.0;
    [bgview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.right.left.equalTo(self);
    }];
    
    
    UIView *lastView = nil;
    for (NSInteger i = 0; i<9; i++) {
        UIView *view = [[ UIView alloc]init];
        [bgview addSubview:view];
        view.backgroundColor = [UIColor colorWithHue:(arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5
                                          brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:1.0];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i%3 == 0) {
                if (lastView) {
                    make.top.equalTo(lastView.mas_bottom).offset(6);
                }else{
                    make.top.equalTo(bgview).offset(6);
                }
                make.left.equalTo(bgview).offset(6);
            }else if (i%3 == 1)
            {
                make.left.equalTo(lastView.mas_right).offset(6);
                make.top.equalTo(lastView.mas_top);
            }else{
                make.left.equalTo(lastView.mas_right).offset(6);
                make.right.equalTo(bgview.mas_right).offset(-6);
                make.top.equalTo(lastView.mas_top);
            }
            make.height.equalTo(@(w));
            make.width.equalTo(@(w));
        }];
        lastView = view;
    }
    [lastView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgview.mas_bottom);
    }];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容