以上是純手寫代碼所經(jīng)歷的關(guān)于頁面布局的三個(gè)時(shí)期
在iphone1-iphone3gs時(shí)代 window的size固定為(320,480) 我們只需要簡單計(jì)算一下相對位置就好了
在iphone4-iphone4s時(shí)代 蘋果推出了retina屏 但是給了碼農(nóng)們非常大的福利:window的size不變
在iphone5-iphone5s時(shí)代 window的size變了(320,568) 這時(shí)autoresizingMask派上了用場(為啥這時(shí)候不用Autolayout? 因?yàn)檫€要支持ios5唄) 簡單的適配一下即可
在iphone6+時(shí)代 window的width也發(fā)生了變化(相對5和5s的屏幕比例沒有變化) 終于是時(shí)候拋棄autoresizingMask改用autolayout了(不用支持ios5了 相對于屏幕適配的多樣性來說autoresizingMask也已經(jīng)過時(shí)了)
Masonry 源碼:https://github.com/Masonry/Masonry
Masonry是一個(gè)輕量級的布局框架 擁有自己的描述語法 采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動布局 簡潔明了 并具有高可讀性 而且同時(shí)支持 iOS 和 Max OS X。
我們先來看一段官方的sample code來認(rèn)識一下Masonry
<pre>[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(superview).with.insets(padding);
}];</pre>
看到block里面的那句話: make edges equalTo superview with insets
通過鏈?zhǔn)降淖匀徽Z言 就把view1給autolayout好了 是不是簡單易懂?
使用
看一下Masonry支持哪一些屬性
<pre>@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;</pre>
這些屬性與NSLayoutAttrubute的對照表如下
http://cc.cocimg.com/api/uploads/20141219/1418956465427751.jpg
其中l(wèi)eading與left trailing與right 在正常情況下是等價(jià)的 但是當(dāng)一些布局是從右至左時(shí)(比如阿拉伯文?沒有類似的經(jīng)驗(yàn)) 則會對調(diào) 換句話說就是基本可以不理不用 用left和right就好了
在ios8發(fā)布后 又新增了一堆奇奇怪怪的屬性(有興趣的朋友可以去瞅瞅) Masonry暫時(shí)還不支持(不過你要支持ios6,ios7 就沒必要去管那么多了)
在講實(shí)例之前 先介紹一個(gè)MACRO
<pre>#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;</pre>
快速的定義一個(gè)weakSelf 當(dāng)然是用于block里面啦 下面進(jìn)入正題(為了方便 我們測試的superView都是一個(gè)size為(300,300)的UIView)
下面 通過一些簡單的實(shí)例來簡單介紹如何輕松愉快的使用Masonry:
-
[基礎(chǔ)] 居中顯示一個(gè)view
<pre>- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.WS(ws);
UIView *sv = [UIView new];
[sv showPlaceHolder];
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));
}];
}</pre>
效果圖
http://cc.cocimg.com/api/uploads/20141219/1418956558366880.png
使用我之間寫的MMPlaceHolder 可以看到superview已經(jīng)按照我們預(yù)期居中并且設(shè)置成了適當(dāng)?shù)拇笮?/p>
那么先看看這幾行代碼
<pre>//從此以后基本可以拋棄CGRectMake了
UIView *sv = [UIView new];
//在做autoLayout之前 一定要先將view添加到superview上 否則會報(bào)錯(cuò)
[self.view addSubview:sv];
//mas_makeConstraints就是Masonry的autolayout添加函數(shù) 將所需的約束添加到block中行了
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
//將sv居中(很容易理解吧?)
make.center.equalTo(ws.view);
//將size設(shè)置成(300,300)
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
</pre>
這里有兩個(gè)問題要分解一下
首先在Masonry中能夠添加autolayout約束有三個(gè)函數(shù)
<pre>- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker make))block;
/
mas_makeConstraints 只負(fù)責(zé)新增約束 Autolayout不能同時(shí)存在兩條針對于同一對象的約束 否則會報(bào)錯(cuò)
mas_updateConstraints 針對上面的情況 會更新在block中出現(xiàn)的約束 不會導(dǎo)致出現(xiàn)兩個(gè)相同約束的情況
mas_remakeConstraints 則會清除之前的所有約束 僅保留最新的約束
三種函數(shù)善加利用 就可以應(yīng)對各種情況了
*/</pre>
其次 equalTo 和 mas_equalTo的區(qū)別在哪里呢? 其實(shí) mas_equalTo是一個(gè)MACRO
<pre>#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)))
</pre>
可以看到 mas_equalTo只是對其參數(shù)進(jìn)行了一個(gè)BOX操作(裝箱) MASBoxValue的定義具體可以看看源代碼 太長就不貼出來了
所支持的類型 除了NSNumber支持的那些數(shù)值類型之外 就只支持CGPoint CGSize UIEdgeInsets
介紹完這幾個(gè)問題 我們就繼續(xù)往下了 PS:剛才定義的sv會成為我們接下來所有sample的superView
-
[初級] 讓一個(gè)view略小于其superView(邊距為10)
<pre>UIView *sv1 = [UIView new];
[sv1 showPlaceHolder];
sv1.backgroundColor = [UIColor redColor];
[sv addSubview:sv1];
[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.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));
*/
}];</pre>
代碼效果
http://cc.cocimg.com/api/uploads/20141219/1418956816349185.png
可以看到 edges 其實(shí)就是top,left,bottom,right的一個(gè)簡化 分開寫也可以 一句話更省事
那么為什么bottom和right里的offset是負(fù)數(shù)呢? 因?yàn)檫@里計(jì)算的是絕對的數(shù)值 計(jì)算的bottom需要小魚sv的底部高度 所以要-10 同理用于right
這里有意思的地方是and和with 其實(shí)這兩個(gè)函數(shù)什么事情都沒做
<pre>- (MASConstraint *)with {
return self;
}
- (MASConstraint *)and {
return self;
}
</pre>
但是用在這種鏈?zhǔn)秸Z法中 就非常的巧妙和易懂 不得不佩服作者的心思(雖然我現(xiàn)在基本都會省略)
- [初級] 讓兩個(gè)高度為150的view垂直居中且等寬且等間隔排列 間隔為10(自動計(jì)算其寬度)
<pre>int padding1 = 10;
[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);
}];</pre>
代碼效果
http://cc.cocimg.com/api/uploads/20141219/1418956895493272.png
這里我們在兩個(gè)子view之間互相設(shè)置的約束 可以看到他們的寬度在約束下自動的被計(jì)算出來了
- [中級] 在UIScrollView順序排列一些view并自動計(jì)算contentSize
<pre>UIScrollView *scrollView = [UIScrollView new];
scrollView.backgroundColor = [UIColor whiteColor];
[sv addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.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);
}];</pre>
http://cc.cocimg.com/api/uploads/20141219/1418956951514750.png
http://cc.cocimg.com/api/uploads/20141219/1418956977254096.png
從scrollView的scrollIndicator可以看出 scrollView的內(nèi)部已如我們所想排列好了
這里的關(guān)鍵就在于container這個(gè)view起到了一個(gè)中間層的作用 能夠自動的計(jì)算uiscrollView的contentSize
- [高級] 橫向或者縱向等間隙的排列一組view
很遺憾 autoLayout并沒有直接提供等間隙排列的方法(Masonry的官方demo中也沒有對應(yīng)的案例) 但是參考案例3 我們可以通過一個(gè)小技巧來實(shí)現(xiàn)這個(gè)目的 為此我寫了一個(gè)Category
<pre>@implementation UIView(Masonry_LJC)
-
(void) distributeSpacingHorizontallyWith:(NSArray*)views
{
NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];for ( int i = 0 ; i < views.count+1 ; ++i )
{
UIView *v = [UIView new];
[spaces addObject:v];
[self addSubview:v];[v mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(v.mas_height); }];}
UIView *v0 = spaces[0];
__weak __typeof(&*self)ws = self;
[v0 mas_makeConstraints:^(MASConstraintMaker make) {
make.left.equalTo(ws.mas_left);
make.centerY.equalTo(((UIView)views[0]).mas_centerY);
}];UIView *lastSpace = v0;
for ( int i = 0 ; i < views.count; ++i )
{
UIView *obj = views[i];
UIView *space = spaces[i+1];[obj mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(lastSpace.mas_right); }]; [space mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(obj.mas_right); make.centerY.equalTo(obj.mas_centerY); make.width.equalTo(v0); }]; lastSpace = space;}
[lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(ws.mas_right);
}];
}
-
(void) distributeSpacingVerticallyWith:(NSArray*)views
{
NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];for ( int i = 0 ; i < views.count+1 ; ++i )
{
UIView *v = [UIView new];
[spaces addObject:v];
[self addSubview:v];[v mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(v.mas_height); }];}
UIView *v0 = spaces[0];
__weak __typeof(&*self)ws = self;
[v0 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(ws.mas_top);
make.centerX.equalTo(((UIView*)views[0]).mas_centerX);
}];
UIView *lastSpace = v0;
for ( int i = 0 ; i < views.count; ++i )
{
UIView *obj = views[i];
UIView *space = spaces[i+1];
[obj mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(lastSpace.mas_bottom);
}];
[space mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(obj.mas_bottom);
make.centerX.equalTo(obj.mas_centerX);
make.height.equalTo(v0);
}];
lastSpace = space;
}
[lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(ws.mas_bottom);
}];
}
@end</pre>
簡單的來測試一下
<pre>UIView *sv11 = [UIView new];
UIView *sv12 = [UIView new];
UIView *sv13 = [UIView new];
UIView *sv21 = [UIView new];
UIView *sv31 = [UIView new];
sv11.backgroundColor = [UIColor redColor];
sv12.backgroundColor = [UIColor redColor];
sv13.backgroundColor = [UIColor redColor];
sv21.backgroundColor = [UIColor redColor];
sv31.backgroundColor = [UIColor redColor];
[sv addSubview:sv11];
[sv addSubview:sv12];
[sv addSubview:sv13];
[sv addSubview:sv21];
[sv addSubview:sv31];
//給予不同的大小 測試效果
[sv11 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(@[sv12,sv13]);
make.centerX.equalTo(@[sv21,sv31]);
make.size.mas_equalTo(CGSizeMake(40, 40));
}];
[sv12 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(70, 20));
}];
[sv13 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(50, 50));
}];
[sv21 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(50, 20));
}];
[sv31 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(40, 60));
}];
[sv distributeSpacingHorizontallyWith:@[sv11,sv12,sv13]];
[sv distributeSpacingVerticallyWith:@[sv11,sv21,sv31]];
[sv showPlaceHolderWithAllSubviews];
[sv hidePlaceHolder];
</pre>
http://cc.cocimg.com/api/uploads/20141219/1418957094320116.png
perfect! 簡潔明了的達(dá)到了我們所要的效果
這里所用的技巧就是 使用空白的占位view來填充我們目標(biāo)view的旁邊 這點(diǎn)通過圖上的空白標(biāo)注可以看出來
小結(jié)
通過以上5個(gè)案例 我覺得已經(jīng)把Masonry的常用功能介紹得差不多了 如果你覺得意猶未盡呢 請下載官方的demo來學(xué)習(xí)
總而言之 Masonry是一個(gè)非常優(yōu)秀的autolayout庫 能夠節(jié)省大量的開發(fā)和學(xué)習(xí)時(shí)間 尤其適合我這種純代碼的iOSer 在iPhone6發(fā)布后引發(fā)的適配潮中 Masonry一定可以助你一臂之力 :)