GitHub鏈接:
https://github.com/SnapKit/Masonry
Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and
expressive syntax. Supports iOS and OSX Auto Layout
基于 AutoLayout NSLayoutConstraints的一個(gè)更加簡化,鏈?zhǔn)胶透菀妆磉_(dá)的語法.支持iOS和OSX自動(dòng)布局.
Masonry
Masonry is still actively maintained, we are committed to fixing bugs and merging
good quality PRs from the wider community.However, if you're using Swift in your
project, we recommend using [SnapKit](https://github.com/SnapKit/SnapKit)
as it provides better type safety with a simpler API.Masonry is a light-weight
layout framework which wraps AutoLayout with a nicer syntax.Masonry has its own
layout DSL which provides a chainable way of describing your NSLayoutConstraints
which results in layout code that is more concise and readable. Masonry supports iOS
and Mac OS X.For examples take a look at the **Masonry iOS Examples** project in the
Masonry workspace. You will need to run pod install after downloading.
Masonry仍在積極維護(hù),我們致力于修復(fù)bug,在他人的幫助下加入了更好的特性。如果,你使用的是Swift在你的項(xiàng)目中,我們建議使用
SnapKit(https://github.com/SnapKit/SnapKit),
因?yàn)樗峁┝艘粋€(gè)更簡單,更好類型的安全API。
Masonry是一個(gè)輕量級(jí)的布局框架,它通過一個(gè)更友好的語法封裝的自動(dòng)布局。Masonry通過本身布局的DSL(Domain-Specific Language可能不準(zhǔn)確),
用一個(gè)鏈?zhǔn)秸Z法描述你的NSLayoutConstraints, 使你的布局代碼更加簡潔易讀.Masonry支持iOS和Mac OS X.
如果你想看看Masonry在iOS的demo工程。您將需要在下載后安裝運(yùn)行。
What's wrong with NSLayoutConstraints?
這個(gè)NSLayoutConstraints到底有什么問題呢?
Under the hood Auto Layout is a powerful and flexible way of organising and laying
out your views. However creating constraints from code is verbose and not very
descriptive. Imagine a simple example in which you want to have a view fill its
superview but inset by 10 pixels on every side
在底層用Auto Layout來組織和布局view是一個(gè)強(qiáng)有力和靈活的方式,然而創(chuàng)建約束代碼冗長,不容易去描述。想象一個(gè)簡單的例子,一個(gè)父視圖上的有一個(gè)子視圖,讓這個(gè)子視圖4個(gè)邊距離父視圖都有10px的大小.
UIView *superview = self;UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[superview addConstraints:@[
//view1 constraints
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:padding.top],
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:padding.left],
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-padding.bottom],
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeRight
multiplier:1
constant:-padding.right],
]];
Even with such a simple example the code needed is quite verbose and quickly becomes
unreadable when you have more than 2 or 3 views. Another option is to use Visual
Format Language (VFL), which is a bit less long winded. However the ASCII type syntax
has its own pitfalls and its also a bit harder to animate as NSLayoutConstraint
constraintsWithVisualFormat:returns an array.
即使是這樣一個(gè)簡單的例子Auto Layout需要的代碼很冗長。很快當(dāng)你有超過2或3次代碼的約束就會(huì)變得晦澀。另一種選擇是使用Visual格式語言(VFL),只是比上面代碼少了一點(diǎn)。但是,ASCII類型的語法都有自己的缺陷和和一點(diǎn)點(diǎn)難度,因?yàn)?br> NSLayoutConstraint constraintsWithVisualFormat動(dòng)畫:返回一個(gè)數(shù)組。
Prepare to meet your Maker!
準(zhǔn)備你一切的需要吧!
Heres the same constraints created using MASConstraintMaker
下面是使用MASConstraintMaker創(chuàng)建相同的約束
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[view1 mas_makeConstraints:^(MASConstraintMaker *make)
{
make.top.equalTo(superview.mas_top).with.offset(padding.top);
//with is an optional semantic filler
make.left.equalTo(superview.mas_left).with.offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];
Or even shorter
甚至更短
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(superview).with.insets(padding);
}];
Also note in the first example we had to add the constraints to the superview
[superview addConstraints:.... Masonry however will automagically add constraints
to the appropriate view.
同時(shí),在第一個(gè)例子中,我們不得不約束添加到superview[superview addConstraints:....但是視圖
會(huì)被Masonry自動(dòng)添加適當(dāng)?shù)募s束.
Masonry will also call
view1.translatesAutoresizingMaskIntoConstraints = NO;for you.
Masonry會(huì)為你調(diào)用
view1.translatesAutoresizingMaskIntoConstraints = NO.