介紹
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的基本介紹就是這樣