天天看點

iOS:Masonry 英文原檔介紹Masonry 英文原檔介紹:

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 <code>pod install</code> after downloading.

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

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 <code>NSLayoutConstraint constraintsWithVisualFormat:</code> returns an array.

Heres the same constraints created using MASConstraintMaker

Or even shorter

Also note in the first example we had to add the constraints to the superview <code>[superview addConstraints:...</code>. Masonry however will automagically add constraints to the appropriate view.

Masonry will also call <code>view1.translatesAutoresizingMaskIntoConstraints = NO;</code> for you.

<code>.equalTo</code> equivalent to NSLayoutRelationEqual <code>.lessThanOrEqualTo</code> equivalent to NSLayoutRelationLessThanOrEqual <code>.greaterThanOrEqualTo</code> equivalent to NSLayoutRelationGreaterThanOrEqual

These three equality constraints accept one argument which can be any of the following:

MASViewAttribute

NSLayoutAttribute

view.mas_left

NSLayoutAttributeLeft

view.mas_right

NSLayoutAttributeRight

view.mas_top

NSLayoutAttributeTop

view.mas_bottom

NSLayoutAttributeBottom

view.mas_leading

NSLayoutAttributeLeading

view.mas_trailing

NSLayoutAttributeTrailing

view.mas_width

NSLayoutAttributeWidth

view.mas_height

NSLayoutAttributeHeight

view.mas_centerX

NSLayoutAttributeCenterX

view.mas_centerY

NSLayoutAttributeCenterY

view.mas_baseline

NSLayoutAttributeBaseline

if you want view.left to be greater than or equal to label.left :

Auto Layout allows width and height to be set to constant values. if you want to set view to have a minimum and maximum width you could pass a number to the equality blocks:

However Auto Layout does not allow alignment attributes such as left, right, centerY etc to be set to constant values. So if you pass a NSNumber for these attributes Masonry will turn these into constraints relative to the view’s superview ie:

Instead of using NSNumber, you can use primitives and structs to build your constraints, like so:

An array of a mixture of any of the previous types

<code>.priority</code> allows you to specify an exact priority <code>.priorityHigh</code> equivalent to UILayoutPriorityDefaultHigh <code>.priorityMedium</code> is half way between high and low <code>.priorityLow</code> equivalent to UILayoutPriorityDefaultLow

Priorities are can be tacked on to the end of a constraint chain like so:

Masonry also gives you a few convenience methods which create multiple constraints at the same time. These are called MASCompositeConstraints

You can chain view attributes for increased readability:

Sometimes you need modify existing constraints in order to animate or remove/replace constraints. In Masonry there are a few different approaches to updating constraints.

You can hold on to a reference of a particular constraint by assigning the result of a constraint make expression to a local variable or a class property. You could also reference multiple constraints by storing them away in an array.

Alternatively if you are only updating the constant value of the constraint you can use the convience method <code>mas_updateConstraints</code> instead of <code>mas_makeConstraints</code>

<code>mas_updateConstraints</code> is useful for updating a set of constraints, but doing anything beyond updating constant values can get exhausting. That's where <code>mas_remakeConstraints</code> comes in.

<code>mas_remakeConstraints</code> is similar to <code>mas_updateConstraints</code>, but instead of updating constant values, it will remove all of its contraints before installing them again. This lets you provide different constraints without having to keep around references to ones which you want to remove.

You can find more detailed examples of all three approaches in the Masonry iOS Examples project.

Laying out your views doesn't always goto plan. So when things literally go pear shaped, you don't want to be looking at console output like this:

Masonry adds a category to NSLayoutConstraint which overrides the default implementation of <code>- (NSString *)description</code>. Now you can give meaningful names to views and constraints, and also easily pick out the constraints created by Masonry.

which means your console output can now look like this:

For an example of how to set this up take a look at the Masonry iOS Examples project in the Masonry workspace.

In your Podfile

<code>pod 'Masonry'</code>

If you want to use masonry without all those pesky 'mas_' prefixes. Add #define MAS_SHORTHAND to your prefix.pch before importing Masonry

<code>#define MAS_SHORTHAND</code>

Get busy Masoning

<code>#import "Masonry.h"</code>

Copy the included code snippets to <code>~/Library/Developer/Xcode/UserData/CodeSnippets</code> to write your masonry blocks at lightning speed!

<code>mas_make</code> -&gt; <code>[&lt;view&gt; mas_makeConstraints:^(MASConstraintMaker *make){&lt;code&gt;}];</code>

<code>mas_update</code> -&gt; <code>[&lt;view&gt; mas_updateConstraints:^(MASConstraintMaker *make){&lt;code&gt;}];</code>

<code>mas_remake</code> -&gt; <code>[&lt;view&gt; mas_remakeConstraints:^(MASConstraintMaker *make){&lt;code&gt;}];</code>

Not limited to subset of Auto Layout. Anything NSLayoutConstraint can do, Masonry can do too!

Great debug support, give your views and constraints meaningful names.

Constraints read like sentences.

No crazy macro magic. Masonry won't pollute the global namespace with macros.

Not string or dictionary based and hence you get compile time checking.

Eye candy

Mac example project

More tests and examples

程式猿神奇的手,每時每刻,這雙手都在改變着世界的互動方式!

本文轉自當天真遇到現實部落格園部落格,原文連結:http://www.cnblogs.com/XYQ-208910/p/5011166.html,如需轉載請自行聯系原作者

繼續閱讀