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的一個更加簡化,鏈式和更容易表達的語法.支持iOS和OSX自動布局.
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仍在積極維護,我們致力于修復bug,在他人的幫助下加入了更好的特性。如果,你使用的是Swift在你的項目中,我們建議使用
SnapKit(https://github.com/SnapKit/SnapKit),
因為它提供了一個更簡單,更好類型的安全API。
Masonry是一個輕量級的布局框架,它通過一個更友好的語法封裝的自動布局。Masonry通過本身布局的DSL(Domain-Specific Language可能不準確),
用一個鏈式語法描述你的NSLayoutConstraints, 使你的布局代碼更加簡潔易讀.Masonry支持iOS和Mac OS X.
如果你想看看Masonry在iOS的demo工程。您將需要在下載后安裝運行。
What's wrong with NSLayoutConstraints?
這個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是一個強有力和靈活的方式,然而創建約束代碼冗長,不容易去描述。想象一個簡單的例子,一個父視圖上的有一個子視圖,讓這個子視圖4個邊距離父視圖都有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.
即使是這樣一個簡單的例子Auto Layout需要的代碼很冗長。很快當你有超過2或3次代碼的約束就會變得晦澀。另一種選擇是使用Visual格式語言(VFL),只是比上面代碼少了一點。但是,ASCII類型的語法都有自己的缺陷和和一點點難度,因為
NSLayoutConstraint constraintsWithVisualFormat動畫:返回一個數組。
Prepare to meet your Maker!
準備你一切的需要吧!
Heres the same constraints created using MASConstraintMaker
下面是使用MASConstraintMaker創建相同的約束
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.
同時,在第一個例子中,我們不得不約束添加到superview[superview addConstraints:....但是視圖
會被Masonry自動添加適當的約束.
Masonry will also call
view1.translatesAutoresizingMaskIntoConstraints = NO;for you.
Masonry會為你調用
view1.translatesAutoresizingMaskIntoConstraints = NO.