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:
| Property | NSLayoutAttribute |
|---|---|
mas_left | NSLayoutAttributeLeft |
mas_right | NSLayoutAttributeRight |
mas_top | NSLayoutAttributeTop |
mas_bottom | NSLayoutAttributeBottom |
mas_leading | NSLayoutAttributeLeading |
mas_trailing | NSLayoutAttributeTrailing |
mas_width | NSLayoutAttributeWidth |
mas_height | NSLayoutAttributeHeight |
mas_centerX | NSLayoutAttributeCenterX |
mas_centerY | NSLayoutAttributeCenterY |
mas_baseline | NSLayoutAttributeBaseline |
mas_firstBaseline | NSLayoutAttributeFirstBaseline |
mas_lastBaseline | NSLayoutAttributeLastBaseline |
Margin Attributes (iOS/tvOS only)
| Property | NSLayoutAttribute |
|---|---|
mas_leftMargin | NSLayoutAttributeLeftMargin |
mas_rightMargin | NSLayoutAttributeRightMargin |
mas_topMargin | NSLayoutAttributeTopMargin |
mas_bottomMargin | NSLayoutAttributeBottomMargin |
mas_leadingMargin | NSLayoutAttributeLeadingMargin |
mas_trailingMargin | NSLayoutAttributeTrailingMargin |
mas_centerXWithinMargins | NSLayoutAttributeCenterXWithinMargins |
mas_centerYWithinMargins | NSLayoutAttributeCenterYWithinMargins |
Safe Area Attributes (iOS 11+ / tvOS 11+)
| Property | Description |
|---|---|
mas_safeAreaLayoutGuide | Safe area layout guide |
mas_safeAreaLayoutGuideLeading | Safe area leading |
mas_safeAreaLayoutGuideTrailing | Safe area trailing |
mas_safeAreaLayoutGuideLeft | Safe area left |
mas_safeAreaLayoutGuideRight | Safe area right |
mas_safeAreaLayoutGuideTop | Safe area top |
mas_safeAreaLayoutGuideBottom | Safe 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
| Property | Type | Description |
|---|---|---|
left | MASConstraint * | Left edge |
top | MASConstraint * | Top edge |
right | MASConstraint * | Right edge |
bottom | MASConstraint * | Bottom edge |
leading | MASConstraint * | Leading edge |
trailing | MASConstraint * | Trailing edge |
width | MASConstraint * | Width |
height | MASConstraint * | Height |
centerX | MASConstraint * | Center X |
centerY | MASConstraint * | Center Y |
baseline | MASConstraint * | Baseline |
Compound Attributes
| Property | Equivalent |
|---|---|
edges | top, left, bottom, right |
size | width, height |
center | centerX, 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
| Method | Description |
|---|---|
.with | Syntactic sugar (no-op, for readability) |
.and | Syntactic 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
| Method | Priority Value |
|---|---|
.priority(MASLayoutPriority) | Custom value |
.priorityRequired() | 1000 |
.priorityHigh() | 750 |
.priorityMedium() | 500 |
.priorityLow() | 250 |
Lifecycle Methods
| Method | Description |
|---|---|
.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
| Property | Type | Description |
|---|---|---|
view | MAS_VIEW * (weak) | The associated view |
item | id (weak) | The layout item |
layoutAttribute | NSLayoutAttribute | The layout attribute |
Methods
| Method | Description |
|---|---|
initWithView:layoutAttribute: | Convenience initializer |
initWithView:item:layoutAttribute: | Designated initializer |
isSizeAttribute | Returns 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);