MasonryMasonry
Guide
  • Objective-C API
  • Swift API
Examples
Changelog
  • English
  • 简体中文
GitHub
Guide
  • Objective-C API
  • Swift API
Examples
Changelog
  • English
  • 简体中文
GitHub
  • API Reference

    • Objective-C API Reference
    • Swift API Reference

Objective-C API Reference

This page documents the core Objective-C classes and categories in the Masonry framework.

View+MASAdditions

The primary category on UIView / NSView that provides constraint creation methods.

View Attributes

Properties that return MASViewAttribute objects for constraint building:

PropertyNSLayoutAttribute
mas_leftNSLayoutAttributeLeft
mas_rightNSLayoutAttributeRight
mas_topNSLayoutAttributeTop
mas_bottomNSLayoutAttributeBottom
mas_leadingNSLayoutAttributeLeading
mas_trailingNSLayoutAttributeTrailing
mas_widthNSLayoutAttributeWidth
mas_heightNSLayoutAttributeHeight
mas_centerXNSLayoutAttributeCenterX
mas_centerYNSLayoutAttributeCenterY
mas_baselineNSLayoutAttributeBaseline
mas_firstBaselineNSLayoutAttributeFirstBaseline
mas_lastBaselineNSLayoutAttributeLastBaseline

Margin Attributes (iOS/tvOS only)

PropertyNSLayoutAttribute
mas_leftMarginNSLayoutAttributeLeftMargin
mas_rightMarginNSLayoutAttributeRightMargin
mas_topMarginNSLayoutAttributeTopMargin
mas_bottomMarginNSLayoutAttributeBottomMargin
mas_leadingMarginNSLayoutAttributeLeadingMargin
mas_trailingMarginNSLayoutAttributeTrailingMargin
mas_centerXWithinMarginsNSLayoutAttributeCenterXWithinMargins
mas_centerYWithinMarginsNSLayoutAttributeCenterYWithinMargins

Safe Area Attributes (iOS 11+ / tvOS 11+)

PropertyDescription
mas_safeAreaLayoutGuideSafe area layout guide
mas_safeAreaLayoutGuideLeadingSafe area leading
mas_safeAreaLayoutGuideTrailingSafe area trailing
mas_safeAreaLayoutGuideLeftSafe area left
mas_safeAreaLayoutGuideRightSafe area right
mas_safeAreaLayoutGuideTopSafe area top
mas_safeAreaLayoutGuideBottomSafe area bottom

Constraint Methods

mas_makeConstraints:

Creates and installs constraints.

- (NSArray<__kindof MASConstraint *> *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

Example:

[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(superview).with.insets(padding);
}];

mas_updateConstraints:

Updates existing constraints (or creates if not found).

- (NSArray<__kindof MASConstraint *> *)mas_updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

mas_remakeConstraints:

Removes all existing Masonry constraints and creates new ones.

- (NSArray<__kindof MASConstraint *> *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

mas_prepareConstraints:

Creates constraints but does NOT install them. Useful for deferred activation.

- (NSArray<__kindof MASConstraint *> *)mas_prepareConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

MASConstraintMaker

The builder object passed into constraint blocks. Provides attribute properties for building constraints.

Attribute Properties

PropertyTypeDescription
leftMASConstraint *Left edge
topMASConstraint *Top edge
rightMASConstraint *Right edge
bottomMASConstraint *Bottom edge
leadingMASConstraint *Leading edge
trailingMASConstraint *Trailing edge
widthMASConstraint *Width
heightMASConstraint *Height
centerXMASConstraint *Center X
centerYMASConstraint *Center Y
baselineMASConstraint *Baseline

Compound Attributes

PropertyEquivalent
edgestop, left, bottom, right
sizewidth, height
centercenterX, centerY

MASConstraint

The constraint object returned by the maker. Supports chaining for relation, offset, priority, etc.

Relation Methods

- (MASConstraint * (^)(id attr))equalTo;
- (MASConstraint * (^)(id attr))greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))lessThanOrEqualTo;

Superview Relation Methods

Set the constraint relation with the corresponding attribute of the view's superview. These methods return a block that takes no arguments, enabling function-call style:

- (MASConstraint * (^)(void))equalToSuperview;
- (MASConstraint * (^)(void))greaterThanOrEqualToSuperview;
- (MASConstraint * (^)(void))lessThanOrEqualToSuperview;

Example:

[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.bottom.equalToSuperview();
    make.width.greaterThanOrEqualToSuperview();
    make.height.lessThanOrEqualToSuperview();
}];

WithLocation Variants

These methods record the call-site file and line number into mas_key for debugging. Used automatically by the mas_ prefixed macros:

- (MASConstraint * (^)(NSString *file, NSUInteger line))equalToSuperviewWithLocation;
- (MASConstraint * (^)(NSString *file, NSUInteger line))greaterThanOrEqualToSuperviewWithLocation;
- (MASConstraint * (^)(NSString *file, NSUInteger line))lessThanOrEqualToSuperviewWithLocation;

Autoboxing Macros

- (MASConstraint * (^)(id attr))mas_equalTo;
- (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;

These macros use the MASBoxValue() macro internally to auto-box scalar and struct values into NSValue. The underlying C function was renamed from _MASBoxValue to masBoxValue in v1.3.6 to comply with naming conventions (identifiers starting with underscore are reserved). The public MASBoxValue() macro interface remains unchanged.

Superview Macros

These macros call the WithLocation variants, automatically embedding file and line info:

mas_equalToSuperview()                    // → equalToSuperviewWithLocation(@(__FILE__), __LINE__)
mas_greaterThanOrEqualToSuperview()       // → greaterThanOrEqualToSuperviewWithLocation(@(__FILE__), __LINE__)
mas_lessThanOrEqualToSuperview()          // → lessThanOrEqualToSuperviewWithLocation(@(__FILE__), __LINE__)

Example:

[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.bottom.mas_equalToSuperview();
    make.width.mas_greaterThanOrEqualToSuperview();
}];

Modifier Methods

MethodDescription
.withSyntactic sugar (no-op, for readability)
.andSyntactic sugar (no-op, for readability)
.offset(CGFloat)Set constant offset
.insets(MASEdgeInsets)Set edge insets
.inset(CGFloat)Set uniform inset
.sizeOffset(CGSize)Set size offset
.centerOffset(CGPoint)Set center offset
.multipliedBy(CGFloat)Set multiplier
.dividedBy(CGFloat)Set multiplier to 1/value
.key(id)Set debug key

Priority Methods

MethodPriority Value
.priority(MASLayoutPriority)Custom value
.priorityRequired()1000
.priorityHigh()750
.priorityMedium()500
.priorityLow()250

Lifecycle Methods

MethodDescription
.install()Install the constraint
.uninstall()Uninstall the constraint
.activate()Activate the constraint
.deactivate()Deactivate the constraint

MASViewAttribute

An immutable tuple storing a view and its related NSLayoutAttribute.

Properties

PropertyTypeDescription
viewMAS_VIEW * (weak)The associated view
itemid (weak)The layout item
layoutAttributeNSLayoutAttributeThe layout attribute

Methods

MethodDescription
initWithView:layoutAttribute:Convenience initializer
initWithView:item:layoutAttribute:Designated initializer
isSizeAttributeReturns YES if width or height

NSArray+MASAdditions

Batch constraint operations on arrays of views.

// Apply constraints to all views in the array
[@[view1, view2, view3] mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@44);
}];

// Distribute views evenly
[@[view1, view2, view3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal
                                    withFixedSpacing:10
                                         leadSpacing:5
                                         tailSpacing:5];

Debugging

Automatic Source-Line Keys

The mas_equalTo(), mas_greaterThanOrEqualTo(), mas_lessThanOrEqualTo(), mas_equalToSuperview(), mas_greaterThanOrEqualToSuperview(), and mas_lessThanOrEqualToSuperview() macros automatically embed the call-site file name and line number into the constraint's mas_key.

Manual Keys

make.top.equalTo(superview.mas_top).offset(20).key(@"topPin");

MASAttachKeys

Tag multiple views at once for debugging:

MASAttachKeys(titleLabel, avatarView, contentView);
Edit this page on GitHub
Last Updated: 3/30/26, 6:37 AM
Contributors: samzhjiang
Next
Swift API Reference