ios開發之 自適應屏九宮格(第三方庫Masonry)

來源:互聯網
上載者:User

標籤:

轉自 http://blog.sina.com.cn/s/blog_13fd67a560102wil5.html

 

    //用來記錄上一次建立的UILabel

    UILabel *lastLabel = nil;

    

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

        

        UILabel *label = [UILabel new];

        label.textAlignment = NSTextAlignmentCenter;

        label.text = [NSString stringWithFormat:@"~%d~",i];

        [self.view addSubview:label];

        

        [label mas_makeConstraints:^(MASConstraintMaker *make) {

            

            //設定高度

            make.height.offset(40.0f);

            

            //計算距離頂部的公式 60 = 上一個距離頂部的高度 + UIlabel的高度

            float colTop = (20 + i/3 * 60.0f);

            

            make.top.offset(colTop);

            

            //當是 左邊一列的時候 都是 距離父視圖 向左邊 20的間隔

            if (i%3 == 0) {

                

                make.left.offset(20.0f);

                

            }else{

                

                //當時中間列的時候 在上一個UIlabel的右邊 添加20個 距離 並且設定等高

                make.width.equalTo(lastLabel.mas_width);

                make.left.equalTo(lastLabel.mas_right).offset(20.0f);

                

            }

           //當是 最右邊列的時候 距離右邊父視圖的 距離為20  因為是向左所以是-20  控制底部也是 負數!!

            if (i%3 == 2) {

                

                make.right.offset(-20.0f);

                

            }

            

        }];

        lastLabel = label;

    }

ios開發之 自適應屏九宮格(第三方庫Masonry)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.