Skip to content

Guide · Open source

How to use Masonry: chainable Auto Layout in Objective-C

Updated · written with AI from the project's own documentation and checked against our catalogue data

Masonry is an open-source layout library for iOS and macOS apps that wraps Auto Layout NSLayoutConstraints in a chainable, shorter syntax. This guide is for developers writing Objective-C code who want to position views in code without long, repetitive constraint calls. After reading it you will know how to make a view fill its superview with insets, use the common attributes, set priorities and composite constraints like edges, size and center.

MasonrySnapKit/Masonry · ★ 18.1k · MIT

Quick start

  1. 1Get the Masonry source. Download the Masonry project from its GitHub repository under the SnapKit organisation.
  2. 2Install CocoaPods dependencies. Run pod install in the examples project folder, as the README instructs.
  3. 3Import Masonry in your view code. Import the Masonry header in the Objective-C file where you build your views. Pick the mas_ prefixed macros by default.
  4. 4Add your view as a subview. Add the view to its superview with addSubview, as you would normally do.
  5. 5Describe constraints with mas_makeConstraints. Call mas_makeConstraints on the view and use the make object to write the chained constraints.
  6. 6Set priorities where needed. Append priorityLow, priorityMedium, priorityHigh or priority(value) to a chain when a constraint should yield to others.

Why use Masonry instead of raw NSLayoutConstraint?

Auto Layout is powerful, but creating constraints from code with NSLayoutConstraint is verbose and not very descriptive. The README's example of pinning a view inset by 10 points on every side takes a long block of constraintWithItem calls. Visual Format Language is shorter, but its ASCII-style strings have their own pitfalls and are harder to animate because constraintsWithVisualFormat returns an array.

Masonry adds its own layout DSL. You describe constraints as a chain, and the same layout becomes more concise and readable.

Lay out a view with mas_makeConstraints

Call mas_makeConstraints on the view you are positioning. Inside the block, the make object describes each constraint. The README notes that Masonry adds the constraints to the appropriate view for you, and it sets translatesAutoresizingMaskIntoConstraints to NO automatically.

  • Write each edge explicitly with make.top, make.left, make.bottom and make.right, using equalTo(superview.mas_top) and offset().
  • Or use the shorter form make.edges.equalTo(superview).with.insets(padding).
[view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(superview).with.insets(padding); }];

Choose the relation and the target

The three relations are equalTo, lessThanOrEqualTo and greaterThanOrEqualTo, which map to NSLayoutRelationEqual, NSLayoutRelationLessThanOrEqual and NSLayoutRelationGreaterThanOrEqual. Each accepts a target of one of four kinds: a MASViewAttribute such as view.mas_left, a UIView or NSView, an NSNumber, or an NSArray mixing these types.

  • MASViewAttribute: make.centerX.lessThanOrEqualTo(view2.mas_left).
  • View: make.left.greaterThanOrEqualTo(label), which is the same as label.mas_left.
  • NSNumber for width and height: make.width.greaterThanOrEqualTo(@200) and make.width.lessThanOrEqualTo(@400).
  • NSNumber for alignment attributes: make.left.lessThanOrEqualTo(@10) creates a constraint relative to the superview, so view.left equals superview.left plus 10.
  • Primitives and structs with mas_ prefix: make.top.mas_equalTo(42), make.size.mas_equalTo(CGSizeMake(50, 100)).
make.width.greaterThanOrEqualTo(@200); make.width.lessThanOrEqualTo(@400);

Set constraint priorities

Add a priority to the end of a constraint chain. The README lists priorityHigh (equivalent to UILayoutPriorityDefaultHigh), priorityMedium (halfway between high and low) and priorityLow (equivalent to UILayoutPriorityDefaultLow). You can also pass an exact value with priority().

make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow(); make.top.equalTo(label.mas_top).with.priority(600);

Use composite constraints for edges, size and center

Masonry provides MASCompositeConstraints that create several constraints at once. edges sets top, left, bottom and right together, size sets width and height, and center sets centerX and centerY. Offsets can be applied with insets, sizeOffset and similar methods.

  • edges.equalTo(view2) matches all four sides.
  • size.greaterThanOrEqualTo(titleLabel) sets a minimum width and height.
  • size.equalTo(superview).sizeOffset(CGSizeMake(100, -50)) adjusts the size relative to the superview.
  • center.equalTo(button1) matches centerX and centerY.
make.center.equalTo(button1)

Run the examples project

The README points to the Masonry iOS Examples project in the Masonry workspace for more examples. You will need to run pod install after downloading. The README's install instruction is only for the examples project, so CocoaPods is the package manager the source mentions.

pod install

What people use it for

  • Pinning a view to its superview with an inset, such as a full-screen card with padding.
  • Setting a minimum or maximum width or height on a label or button.
  • Centring a view on another view with center.equalTo.
  • Keeping a view at least as wide as a label with greaterThanOrEqualTo.
  • Maintaining an existing Objective-C codebase that already uses Masonry.

Frequently asked questions

Is Masonry free to use?

Masonry is released under the MIT licence, as stated in the project information. Check the LICENSE file in the repository for the full terms before shipping.

How do I install Masonry?

The README covers installation through CocoaPods. It explicitly says to run pod install for the examples project after downloading. For your own app, follow the package-manager setup in the repository.

Does Masonry work with Swift?

The README says that if you use Swift, it recommends SnapKit instead because it offers better type safety with a simpler API. Masonry is aimed at Objective-C projects.

Does Masonry work with storyboards?

Masonry is aimed at code-based constraints. If you lay out views in Interface Builder or storyboards, it is not the intended fit.

What is the difference between Masonry and SnapKit?

Both wrap Auto Layout in a chainable syntax. The README recommends SnapKit for Swift projects because of its type safety and simpler API, while Masonry targets Objective-C. Other alternatives named in the source include PureLayout and raw NSLayoutConstraint.

Why does my view not appear where I expected?

The source does not list troubleshooting steps. Check that the constraint targets are in the same view hierarchy, and review priorities, since lower-priority constraints can be overridden. The examples project is the best reference.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Masonry suits iOS or macOS developers writing Objective-C who use Auto Layout in code and want shorter constraint code without Visual Format Language strings. Swift-only teams should look at SnapKit, which the README recommends, and people who build layouts in Interface Builder or storyboards should pick a different approach.

Install options & downloads