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:
| Property | Description |
|---|---|
view.mas.left | Left edge |
view.mas.right | Right edge |
view.mas.top | Top edge |
view.mas.bottom | Bottom edge |
view.mas.leading | Leading edge |
view.mas.trailing | Trailing edge |
view.mas.width | Width |
view.mas.height | Height |
view.mas.centerX | Center X |
view.mas.centerY | Center Y |
view.mas.baseline | Baseline |
Safe Area Attributes (iOS 11+)
| Property | Description |
|---|---|
view.mas.safeAreaLayoutGuideTop | Safe area top |
view.mas.safeAreaLayoutGuideBottom | Safe area bottom |
view.mas.safeAreaLayoutGuideLeading | Safe area leading |
view.mas.safeAreaLayoutGuideTrailing | Safe 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
| Operator | Relation | Supported RHS Types |
|---|---|---|
== | Equal | ViewAttribute, UIView/NSView, CGFloat, Int, Double, CGSize, CGPoint, UIEdgeInsets |
>= | Greater than or equal | Same as above |
<= | Less than or equal | Same 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
| Operator | Description | Example |
|---|---|---|
+ | Add offset | view.mas.top + 20 |
- | Subtract offset | view.mas.bottom - 10 |
* | Multiply | view.mas.width * 0.5 |
/ | Divide | view.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 { ... }