Masonry簡(jiǎn)介

介紹

Masonry是一個(gè)輕量級(jí)的布局框架 擁有自己的描述語(yǔ)法 采用更優(yōu)雅的鏈?zhǔn)秸Z(yǔ)法封裝自動(dòng)布局 簡(jiǎn)潔明了 并具有高可讀性 而且同時(shí)支持 iOS 和 Max OS X刻获。可以通過cocoapods將其導(dǎo)入炕婶。

使用

Masonry屬性及其說明

//左側(cè)

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;

//上側(cè)

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;

//右側(cè)

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;

//下側(cè)

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;

//首部

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;

//尾部

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;

//寬度

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;

//高度

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;

//橫向中點(diǎn)

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;

//縱向中點(diǎn)

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;

//文本基線

//@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;

其中l(wèi)eading與left trailing與right 在正常情況下是等價(jià)的 但是當(dāng)一些布局是從右至左時(shí)(比如阿拉伯文?沒有類似的經(jīng)驗(yàn)) 則會(huì)對(duì)調(diào) 換句話說就是基本可以不理不用 用left和right就好了

首先,宏定義一個(gè)self用于Block內(nèi)部莱预,防止循環(huán)引用

#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

下面通過實(shí)例進(jìn)行介紹

1.居中顯示一個(gè)View

WS(ws);

UIView *sv = [UIView new];

sv.backgroundColor = [UIColor blackColor];

[self.view addSubview:sv];

[sv mas_makeConstraints:^(MASConstraintMaker *make) {

make.center.equalTo(ws.view);

make.size.mas_equalTo(CGSizeMake(300, 300));

}];

代碼效果:



首先在Masonry中能夠添加autolayout約束有三個(gè)函數(shù)

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;

/**

*? Creates a MASConstraintMaker with the callee view.

*? Any constraints defined are added to the view or the appropriate superview once the block has finished executing.

*? If an existing constraint exists then it will be updated instead.

*

*? @param block scope within which you can build up the constraints which you wish to apply to the view.

*

*? @return Array of created/updated MASConstraints

*/

- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;

/**

*? Creates a MASConstraintMaker with the callee view.

*? Any constraints defined are added to the view or the appropriate superview once the block has finished executing.

*? All constraints previously installed for the view will be removed.

*

*? @param block scope within which you can build up the constraints which you wish to apply to the view.

*

*? @return Array of created/updated MASConstraints

*/

- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

mas_makeConstraints 只負(fù)責(zé)新增約束 Autolayout不能同時(shí)存在兩條針對(duì)于同一對(duì)象的約束 否則會(huì)報(bào)錯(cuò)

mas_updateConstraints?針對(duì)上面的情況?會(huì)更新在block中出現(xiàn)的約束?不會(huì)導(dǎo)致出現(xiàn)兩個(gè)相同約束的情況

mas_remakeConstraints?則會(huì)清除之前的所有約束?僅保留最新的約束

三種函數(shù)善加利用?就可以應(yīng)對(duì)各種情況了

其次 equalTo 和 mas_equalTo的區(qū)別在哪里呢? 其實(shí) mas_equalTo是一個(gè)MACRO

#define mas_equalTo(...)? ? ? ? ? ? ? ? equalTo(MASBoxValue((__VA_ARGS__)))

#define mas_greaterThanOrEqualTo(...)? ? greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))

#define mas_lessThanOrEqualTo(...)? ? ? lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))

#define mas_offset(...)? ? ? ? ? ? ? ? ? valueOffset(MASBoxValue((__VA_ARGS__)))

#ifdef MAS_SHORTHAND_GLOBALS

#define equalTo(...)? ? ? ? ? ? ? ? ? ? mas_equalTo(__VA_ARGS__)

#define greaterThanOrEqualTo(...)? ? ? ? mas_greaterThanOrEqualTo(__VA_ARGS__)

#define lessThanOrEqualTo(...)? ? ? ? ? mas_lessThanOrEqualTo(__VA_ARGS__)

#define offset(...)? ? ? ? ? ? ? ? ? ? ? mas_offset(__VA_ARGS__)

#endif

可以看到 mas_equalTo只是對(duì)其參數(shù)進(jìn)行了一個(gè)BOX操作(裝箱) MASBoxValue的定義具體可以看看源代碼 太長(zhǎng)就不貼出來了

所支持的類型 除了NSNumber支持的那些數(shù)值類型之外 就只支持CGPoint CGSize UIEdgeInsets

2.讓一個(gè)View略小于其SuperView(邊距10)

UIView *sv1 = [UIView new];

sv1.backgroundColor = [UIColor redColor];

[sv addSubview:sv1];

[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {

make.edges.equalTo(sv).width.insets(UIEdgeInsetsMake(10, 10, 10, 10));

/*等價(jià)于

make.top.equalTo(sv).with.offset(10);

make.left.equalTo(sv).with.offset(10);

make.bottom.equalTo(sv).with.offset(-10);

make.right.equalTo(sv).with.offset(-10);

*/

/*也等價(jià)于

make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

*/

}];

代碼效果

可以看到 edges 其實(shí)就是top,left,bottom,right的一個(gè)簡(jiǎn)化 分開寫也可以 一句話更省事

那么為什么bottom和right里的offset是負(fù)數(shù)呢? 因?yàn)檫@里計(jì)算的是絕對(duì)的數(shù)值 計(jì)算的bottom需要小魚sv的底部高度 所以要-10 同理用于right

3.讓兩個(gè)高度為150的view垂直居中且等寬且等間隔排列 間隔為10(自動(dòng)計(jì)算其寬度)

int padding1 = 10;

UIView *sv2 = [UIView new];

sv2.backgroundColor = [UIColor yellowColor];

[sv addSubview:sv2];

UIView *sv3 = [UIView new];

sv3.backgroundColor = [UIColor yellowColor];

[sv addSubview:sv3];

[sv2 mas_makeConstraints:^(MASConstraintMaker *make) {

make.centerY.mas_equalTo(sv.mas_centerY);

make.left.equalTo(sv.mas_left).with.offset(padding1);

make.right.equalTo(sv3.mas_left).with.offset(-padding1);

make.height.mas_equalTo(@150);

make.width.equalTo(sv3);

}];

[sv3 mas_makeConstraints:^(MASConstraintMaker *make) {

make.centerY.mas_equalTo(sv.mas_centerY);

make.left.equalTo(sv2.mas_right).with.offset(padding1);

make.right.equalTo(sv.mas_right).with.offset(-padding1);

make.height.mas_equalTo(@150);

make.width.equalTo(sv2);

}];

代碼效果

這里我們?cè)趦蓚€(gè)子view之間互相設(shè)置的約束 可以看到他們的寬度在約束下自動(dòng)的被計(jì)算出來了

4.在UIScrollView順序排列一些View并自動(dòng)計(jì)算contentSize

UIScrollView *scrollView = [UIScrollView new];

scrollView.backgroundColor = [UIColor whiteColor];

[sv addSubview:scrollView];

[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {

make.edges.equalTo(sv).width.insets(UIEdgeInsetsMake(5, 5, 5, 5));

}];

UIView *container = [UIView new];

[scrollView addSubview:container];

[container mas_makeConstraints:^(MASConstraintMaker *make) {

make.edges.equalTo(scrollView);

make.width.equalTo(scrollView);

}];

int count = 10;

UIView *lastView = nil;

for (int i = 1; i <= count; ++i) {

UIView *subv = [UIView new];

[container addSubview:subv];

subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )

saturation:( arc4random() % 128 / 256.0 ) + 0.5

brightness:( arc4random() % 128 / 256.0 ) + 0.5

alpha:1];

[subv mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.and.right.equalTo(container);

make.height.mas_equalTo(@(20 * i));

if (lastView) {

make.top.mas_equalTo(lastView.mas_bottom);

}else{

make.top.mas_equalTo(container.mas_top);

}

}];

lastView = subv;

}

[container mas_makeConstraints:^(MASConstraintMaker *make) {

make.bottom.equalTo(lastView.mas_bottom);

}];

頭部效果

尾部效果

從scrollView的scrollIndicator可以看出 scrollView的內(nèi)部已如我們所想排列好了

這里的關(guān)鍵就在于container這個(gè)view起到了一個(gè)中間層的作用 能夠自動(dòng)的計(jì)算uiscrollView的contentSize

Masonry的基本介紹就是這樣

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末柠掂,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子依沮,更是在濱河造成了極大的恐慌陪踩,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件悉抵,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡摘完,警方通過查閱死者的電腦和手機(jī)姥饰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來孝治,“玉大人列粪,你說我怎么就攤上這事√胳” “怎么了岂座?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)杭措。 經(jīng)常有香客問我费什,道長(zhǎng),這世上最難降的妖魔是什么手素? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任鸳址,我火速辦了婚禮瘩蚪,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘稿黍。我一直安慰自己疹瘦,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布巡球。 她就那樣靜靜地躺著言沐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪酣栈。 梳的紋絲不亂的頭發(fā)上险胰,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音钉嘹,去河邊找鬼鸯乃。 笑死,一個(gè)胖子當(dāng)著我的面吹牛跋涣,可吹牛的內(nèi)容都是我干的缨睡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼陈辱,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼奖年!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起沛贪,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤陋守,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后利赋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體水评,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年媚送,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了中燥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡塘偎,死狀恐怖疗涉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吟秩,我是刑警寧澤咱扣,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站涵防,受9級(jí)特大地震影響闹伪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一祭往、第九天 我趴在偏房一處隱蔽的房頂上張望伦意。 院中可真熱鬧,春花似錦硼补、人聲如沸驮肉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)离钝。三九已至,卻和暖如春褪储,著一層夾襖步出監(jiān)牢的瞬間卵渴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工鲤竹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留浪读,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓辛藻,卻偏偏與公主長(zhǎng)得像碘橘,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吱肌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容