生活随笔
收集整理的這篇文章主要介紹了
iOS开发之第三方框架Masonry
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
第三方框架Masonry
該框架可以大大簡化AutoLayout使用過程中對控件添加約束的代碼。 框架地址:https://github.com/SnapKit/Masonry
- 使用步驟
- 添加Masonry文件夾的所有源代碼到項(xiàng)目中
- 添加2個(gè)宏、導(dǎo)入主頭文件
// 只要添加了這個(gè)宏,就不用帶mas_前綴
#define MAS_SHORTHAND
// 只要添加了這個(gè)宏,equalTo就等價(jià)于mas_equalTo
#define MAS_SHORTHAND_GLOBALS
// 這個(gè)頭文件一定要放在上面兩個(gè)宏的后面
#import "Masonry.h"
- mas_equalTo和equalTo的區(qū)別: 默認(rèn)情況下,mas_equalTo有自動包裝功能,比如自動將20包裝為@20,equalTo沒有自動包裝功能,如果添加了下面的宏,那么mas_equalTo和equalTo就沒有區(qū)別
#define MAS_SHORTHAND_GLOBALS
// 這個(gè)方法只會添加新的約束[view makeConstraints:^(MASConstraintMaker *make) {}];// 這個(gè)方法會將以前的所有約束刪掉,添加新的約束[view remakeConstraints:^(MASConstraintMaker *make) {}];// 這個(gè)方法將會覆蓋以前的某些特定的約束[view updateConstraints:^(MASConstraintMaker *make) {}];
1.尺寸:width\height\size
2.邊界:left\leading\right\trailing\top\bottom
3.中心點(diǎn):center\centerX\centerY
4.邊界:edges
// 藍(lán)色控件UIView *blueView = [[UIView alloc] init];blueView.backgroundColor = [UIColor blueColor];[self.view addSubview:blueView];// 居中(水平+垂直)// 尺寸是父控件的一半[blueView mas_makeConstraints:^(MASConstraintMaker *make) {make.size.mas_equalTo(self.view).multipliedBy(0.5);make.center.mas_equalTo(self.view);//make.centerX.mas_equalTo(self.view);//make.centerY.mas_equalTo(self.view);}];
// 藍(lán)色控件UIView *blueView = [[UIView alloc] init];blueView.backgroundColor = [UIColor blueColor];[self.view addSubview:blueView];// 紅色控件UIView *redView = [[UIView alloc] init];redView.backgroundColor = [UIColor redColor];[self.view addSubview:redView];// 添加約束CGFloat margin = 20;CGFloat height = 50;[blueView makeConstraints:^(MASConstraintMaker *make) {//make就是指前面調(diào)用makeConstraints的對象make.left.equalTo(self.view.left).offset(margin);make.right.equalTo(redView.left).offset(-margin);make.bottom.equalTo(self.view.bottom).offset(-margin);make.height.equalTo(height);make.top.equalTo(redView.top);make.bottom.equalTo(redView.bottom);make.width.equalTo(redView.width);}];[redView makeConstraints:^(MASConstraintMaker *make) {make.right.equalTo(self.view.right).offset(-margin);}];
轉(zhuǎn)載于:https://my.oschina.net/shenhuniurou/blog/633153
總結(jié)
以上是生活随笔為你收集整理的iOS开发之第三方框架Masonry的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。