IOS控制項布局之Masonry布局架構

來源:互聯網
上載者:User

標籤:android   windows   iphone5   開發   而且   

    

前言:

  回想起2013年做iOS開發的時候,那時候並沒有採用手寫布局代碼的方式,而是採用xib檔案來編寫,如果使用純程式碼方式是基於window的size(320,480)計算出一個相對位置進行布局,那個時候windows的size是固定不變的,隨著iphone5的發布,windows的size(320,568)也發生了變化,而採用autoresizingMask的方式進行適配,到後來iphone 6之後windows size的寬度也隨之變化,開始拋棄autoresizingMask改用autolayout了,使用autolayout進行適配我也是最近重新做iOS開發才接觸的,公司使用Masonry架構進行布局適配。所以學習使用這個布局架構對我來說至關重要,它大大提高了開發效率而且最近使用起來很多文法和Android有很大的相似之處。

什麼是Masonry?

  Masonry是一個輕量級的布局架構,擁有自己的描述文法,採用更優雅的鏈式文法封裝自動布局、簡潔明了、 並具有高可讀性、 而且同時支援 iOS 和 Max OS X。

如何使用?1.)引入標頭檔

我這裡是在全域引用pch檔案中引用的

#import "Masonry.h"
2.)基本文法

Masonry提供的屬性

  • @property (nonatomic, strong, readonly) MASConstraint *left;//左側

  • @property (nonatomic, strong, readonly) MASConstraint *top;//上側

  • @property (nonatomic, strong, readonly) MASConstraint *right;//右側

  • @property (nonatomic, strong, readonly) MASConstraint *bottom;//下側

  • @property (nonatomic, strong, readonly) MASConstraint *leading;//首部

  • @property (nonatomic, strong, readonly) MASConstraint *trailing;//尾部

  • @property (nonatomic, strong, readonly) MASConstraint *width;//寬

  • @property (nonatomic, strong, readonly) MASConstraint *height;//高

  • @property (nonatomic, strong, readonly) MASConstraint *centerX;//橫向置中

  • @property (nonatomic, strong, readonly) MASConstraint *centerY;//縱向置中

  • @property (nonatomic, strong, readonly) MASConstraint *baseline;//文本基準

Masonry提供了三個函數方法

  • - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //新增約束

  • - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//更新約束

  • - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;//清楚之前的所有約束,只會保留最新的約束

我們根據不同的使用情境來選擇使用不同的函數方法。

3.)具體舉例

  比如一個往父控制項中添加一個上下左右與父控制項間距為50的子視圖

添加約束

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

    UIView *tempView=[[UIView alloc]init];    tempView.backgroundColor=[UIColor greenColor];    [self.view addSubview:tempView];        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {        make.left.mas_equalTo(50);        make.right.mas_equalTo(-50);        make.top.mas_equalTo(50);        make.bottom.mas_equalTo(-50);    }];

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

等價於

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

    UIView *tempView=[[UIView alloc]init];    tempView.backgroundColor=[UIColor greenColor];    [self.view addSubview:tempView];        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {        make.left.equalTo(self.view.mas_left).offset(50);        make.right.equalTo(self.view.mas_right).offset(-50);        make.top.equalTo(self.view.mas_top).offset(50);        make.bottom.equalTo(self.view.mas_bottom).offset(-50);    }];

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

也可以簡化為下面這種

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

    UIView *tempView=[[UIView alloc]init];    tempView.backgroundColor=[UIColor greenColor];    [self.view addSubview:tempView];        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {        make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50));    }];

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

又等價於

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

    UIView *tempView=[[UIView alloc]init];    tempView.backgroundColor=[UIColor greenColor];    [self.view addSubview:tempView];        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));    }];

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

更新約束

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

    [tempView mas_updateConstraints:^(MASConstraintMaker *make) {        make.left.mas_equalTo(50);        make.right.mas_equalTo(-50);        make.top.mas_equalTo(100);        make.bottom.mas_equalTo(-100);    }];

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

清除之前的約束保留最新的

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

    [tempView mas_remakeConstraints:^(MASConstraintMaker *make) {        make.left.mas_equalTo(100);        make.right.mas_equalTo(-100);        make.top.mas_equalTo(100);        make.bottom.mas_equalTo(-100);    }];

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

特別注意:

   聲明約束必須在視圖添加到父試圖上面之後調用。

4.)mas_equalTo與equalTo

 上面的舉例中分別使用了mas_equalTo和equalTo達到了同樣的效果,我在剛開始使用Masonry的時候很容易混淆他們兩個,今天特意分析一下他們的區別。mas_equalTo是一個MACRO,比較的是值,equalTo比較的是id類型。


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.