Need help with Masonry?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

18.0K Stars 3.2K Forks MIT License 406 Commits 145 Opened issues


Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout

Services available


Need anything else?

Contributors list

Masonry Build Status Coverage Status Carthage compatible Pod Version

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 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.

What's wrong with 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 ```obj-c UIView *superview = self.view;

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

[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.

Prepare to meet your Maker!

Heres the same constraints created using MASConstraintMaker

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {; //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) {

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.

Masonry will also call

view1.translatesAutoresizingMaskIntoConstraints = NO;
for you.

Not all things are created equal

equivalent to NSLayoutRelationEqual

equivalent to NSLayoutRelationLessThanOrEqual

equivalent to NSLayoutRelationGreaterThanOrEqual

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

1. MASViewAttribute


MASViewAttribute | NSLayoutAttribute ------------------------- | -------------------------- view.masleft | NSLayoutAttributeLeft view.masright | NSLayoutAttributeRight view.mastop | NSLayoutAttributeTop view.masbottom | NSLayoutAttributeBottom view.masleading | NSLayoutAttributeLeading view.mastrailing | NSLayoutAttributeTrailing view.maswidth | NSLayoutAttributeWidth view.masheight | NSLayoutAttributeHeight view.mascenterX | NSLayoutAttributeCenterX view.mascenterY | NSLayoutAttributeCenterY view.mas_baseline | NSLayoutAttributeBaseline

2. UIView/NSView

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

//these two constraints are exactly the same

3. NSNumber

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:

//width >= 200 && width <= 400

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:

//creates view.left = view.superview.left + 10

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

make.size.mas_equalTo(CGSizeMake(50, 100));
make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));

By default, macros which support autoboxing are prefixed with

. Unprefixed versions are available by defining
before importing Masonry.

4. NSArray

An array of a mixture of any of the previous types

make.height.equalTo(@[view1.mas_height, view2.mas_height]);
make.height.equalTo(@[view1, view2]);
make.left.equalTo(@[view1, @100, view3.right]);

Learn to prioritize

allows you to specify an exact priority

equivalent to UILayoutPriorityDefaultHigh

is half way between high and low

equivalent to UILayoutPriorityDefaultLow

Priorities are can be tacked on to the end of a constraint chain like so: ```obj-c make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();; ```

Composition, composition, composition

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


// make top, left, bottom, right equal view2

// make top = + 5, left = superview.left + 10, // bottom = superview.bottom - 15, right = superview.right - 20 make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))


// make width and height greater than or equal to titleLabel

// make width = superview.width + 100, height = superview.height - 50 make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))


// make centerX and centerY = button1

// make centerX = superview.centerX - 5, centerY = superview.centerY + 10, 10))

You can chain view attributes for increased readability:

// All edges but the top should equal those of the superview

Hold on for dear life

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.

1. References

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.

// in public/private interface
@property (nonatomic, strong) MASConstraint *topConstraint;


// when making constraints [view1 mas_makeConstraints:^(MASConstraintMaker *make) { self.topConstraint =; make.left.equalTo(superview.mas_left).with.offset(padding.left); }];

... // then later you can call [self.topConstraint uninstall];

2. mas_updateConstraints

Alternatively if you are only updating the constant value of the constraint you can use the convience method

instead of
// this is Apple's recommended place for adding/updating constraints
// this method can get called multiple times in response to setNeedsUpdateConstraints
// which can be called by UIKit internally or in your code if you need to trigger an update to your constraints
- (void)updateConstraints {
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {;

//according to apple super should be called at end of method
[super updateConstraints];


3. mas_remakeConstraints

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

is similar to
, but instead of updating constant values, it will remove all of its constraints before installing them again. This lets you provide different constraints without having to keep around references to ones which you want to remove.
- (void)changeButtonPosition {
    [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {

    if (topLeft) {;
    } else {


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

When the ^&*[email protected] hits the fan!

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:

Unable to simultaneously satisfy constraints.....blah blah blah....
    "<0x7189ac0 v:>=5000)]>",
    "<0x839ea20 h="--&" v="--&" v:>",
    "<0x7189c70 uilabel:0x7186980.bottom="=" masexampledebuggingview:0x7186560.bottom>",
    "<0x7189560 v:>"

Will attempt to recover by breaking constraint <0x7189ac0 v:>=5000)]> </0x7189ac0></0x7189560></0x7189c70></0x839ea20></0x7189ac0>

Masonry adds a category to NSLayoutConstraint which overrides the default implementation of

- (NSString *)description
. 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:

Unable to simultaneously satisfy constraints......blah blah blah....
    "<0x8887740 masexampledebuggingview:superview.height="=">",
    "= 5000>",

Will attempt to recover by breaking constraint = 5000> </0x8887740>

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

Where should I create my constraints?

@implementation DIYCustomView

  • (id)init { self = [super init]; if (!self) return nil;

    // --- Create your views here --- self.button = [[UIButton alloc] init];

    return self; }

// tell UIKit that you are using AutoLayout

  • (BOOL)requiresConstraintBasedLayout { return YES; }

// this is Apple's recommended place for adding/updating constraints

  • (void)updateConstraints {

    // --- remake/update constraints here [self.button remakeConstraints:^(MASConstraintMaker *make) {



    //according to apple super should be called at end of method [super updateConstraints]; }

  • (void)didTapButton:(UIButton *)button { // --- Do your changes ie change variables that affect your layout etc --- self.buttonSize = CGSize(200, 200);

    // tell constraints they need updating [self setNeedsUpdateConstraints]; }



Use the orsome CocoaPods.

In your Podfile

pod 'Masonry'

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


Get busy Masoning

#import "Masonry.h"

Code Snippets

Copy the included code snippets to

to write your masonry blocks at lightning speed!

[ mas_makeConstraints:^(MASConstraintMaker *make) {


[ mas_updateConstraints:^(MASConstraintMaker *make) {


[ mas_remakeConstraints:^(MASConstraintMaker *make) {



  • 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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.