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

Swift API Reference

The MasonrySwift module provides a type-safe, Swift-native DSL for Masonry. All APIs are accessed through the view.mas namespace.

MASViewDSL (view.mas)

The primary entry point for Swift constraint building.

Constraint Methods

makeConstraints(_:)

Creates and installs constraints.

@discardableResult
func makeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) -> [Constraint]

Example:

view.mas.makeConstraints { make in
    make.edges.equalTo(superview).inset(16)
}

updateConstraints(_:)

Updates existing constraints (or creates if not found).

@discardableResult
func updateConstraints(_ closure: (_ make: ConstraintMaker) -> Void) -> [Constraint]

remakeConstraints(_:)

Removes all existing Masonry constraints and creates new ones.

@discardableResult
func remakeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) -> [Constraint]

prepareConstraints(_:)

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

@discardableResult
func prepareConstraints(_ closure: (_ make: ConstraintMaker) -> Void) -> [Constraint]

removeConstraints()

Removes all Masonry-installed constraints from the view.

func removeConstraints()

View Attributes (view.mas.*)

Access view attributes through the mas namespace:

PropertyDescription
view.mas.leftLeft edge
view.mas.rightRight edge
view.mas.topTop edge
view.mas.bottomBottom edge
view.mas.leadingLeading edge
view.mas.trailingTrailing edge
view.mas.widthWidth
view.mas.heightHeight
view.mas.centerXCenter X
view.mas.centerYCenter Y
view.mas.baselineBaseline

Safe Area Attributes (iOS 11+)

PropertyDescription
view.mas.safeAreaLayoutGuideTopSafe area top
view.mas.safeAreaLayoutGuideBottomSafe area bottom
view.mas.safeAreaLayoutGuideLeadingSafe area leading
view.mas.safeAreaLayoutGuideTrailingSafe area trailing

Constraint Relation Methods

equalTo(_:)

@discardableResult
func equalTo(_ value: Any) -> Constraint

greaterThanOrEqualTo(_:)

@discardableResult
func greaterThanOrEqualTo(_ value: Any) -> Constraint

lessThanOrEqualTo(_:)

@discardableResult
func lessThanOrEqualTo(_ value: Any) -> Constraint

Superview Shortcuts

These methods automatically capture the call-site file and line number for debugging:

@discardableResult
func equalToSuperview(_ file: String = #fileID, _ line: UInt = #line) -> Constraint

@discardableResult
func greaterThanOrEqualToSuperview(_ file: String = #fileID, _ line: UInt = #line) -> Constraint

@discardableResult
func lessThanOrEqualToSuperview(_ file: String = #fileID, _ line: UInt = #line) -> Constraint

Example:

view.mas.makeConstraints { make in
    make.top.left.bottom.equalToSuperview()
    make.width.greaterThanOrEqualToSuperview()
    make.height.lessThanOrEqualToSuperview().offset(-20)
}

Tips

The file and line parameters use default values and are transparent to callers. Debug information is automatically embedded into the constraint's key.


Operators

The Swift module supports operator-based constraint creation for a more natural syntax.

Relation Operators

OperatorRelationSupported RHS Types
==EqualViewAttribute, UIView/NSView, CGFloat, Int, Double, CGSize, CGPoint, UIEdgeInsets
>=Greater than or equalSame as above
<=Less than or equalSame as above

Examples:

view.mas.makeConstraints { make in
    // ViewAttribute
    make.top == superview.mas.top + 20
    make.left >= superview.mas.left + 16

    // Numeric values
    make.width == 200
    make.height <= 44

    // CGSize
    make.size == CGSize(width: 100, height: 50)

    // UIEdgeInsets
    make.edges == UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}

Arithmetic Operators

OperatorDescriptionExample
+Add offsetview.mas.top + 20
-Subtract offsetview.mas.bottom - 10
*Multiplyview.mas.width * 0.5
/Divideview.mas.height / 2

Priority Operator

Use ~ to set constraint priority:

make.height == 44 ~ .defaultHigh
make.width <= 200 ~ 600

Convenience Extensions on UIView

For developers who prefer a more concise API without the .mas namespace:

// These are equivalent:
view.mas.makeConstraints { ... }
view.masMakeConstraints { ... }

view.mas.updateConstraints { ... }
view.masUpdateConstraints { ... }

view.mas.remakeConstraints { ... }
view.masRemakeConstraints { ... }

Array Extensions

Batch operations on arrays of views:

let views = [view1, view2, view3]

// Apply constraints to all views
views.mas_makeConstraints { make in
    make.height == 44
}

// Update constraints for all views
views.mas_updateConstraints { make in
    make.height == 88
}

// Distribute views evenly
views.mas_distributeViews(along: .horizontal,
                          withFixedSpacing: 10,
                          leadSpacing: 5,
                          tailSpacing: 5)

Constraint Lifecycle

// Create but don't install
let constraints = view.mas.prepareConstraints { make in
    make.width.equalTo(100)
    make.height.equalTo(80)
}

// Activate later
constraints.forEach { $0.activate() }

// Deactivate
constraints.forEach { $0.deactivate() }

// Check if active
if constraint.isActive { ... }
Edit this page on GitHub
Last Updated: 3/30/26, 2:11 AM
Contributors: samzhjiang
Prev
Objective-C API Reference