MondrianLayout - Structured layout for AutoLayout
??♂️?
We guess we still don't cover the all of use-cases.
Please feel free to ask what you've faced case in Issues!
Describing the layout ergonomically in the code
Structured Layout API (DSL)
To keep flexibility of AutoLayout, we have classical style API
Classical Layout API

This image laid out by MondrianLayout
Layout code
Structured layout API and Classical layout API
- ? Ergonomically Enables us to describe layout by DSL (like SwiftUI's layout).
- ? Automatic adding subviews according to layout representation.
- ? Supports integeration with system AutoLayout API.
- ? Provides classical layout API that describing constraints each view.
Introduction
A DSL based layout builder with AutoLayout
AutoLayout is super powerful to describe the layout and how it changes according to the bounding box.
What if we get a more ergonomic interface to declare the constraints.
Structured layout API
![]() |
![]() |

Unstructured layout API (classic style)

Future direction
- Optimize the code - still verbose implementation because we're not sure if the API can be stable.
- Brushing up the DSL - to be stable in describing.
- Adding more modifiers for fine tuning in layout.
- Tuning up the stack block's behavior.
- Adding a way to setting constraints independently besides DSL
- AutoLayout is definitely powerful to describe the layout. We might need to set the constraints additionally since DSL can't describe every pattern of the layout.
Demo app
You can see many layout examples from the demo application.
https://user-images.githubusercontent.com/1888355/122651186-142e7d00-d172-11eb-8bde-f4432d0a0ac9.mp4
Overview
MondrianLayout enables us to describe layouts of subviews by DSL (powered by resultBuilders
)
It's like describing in SwiftUI, but this behavior differs a bit since laying out by AutoLayout system.
To describe layout, use buildSubviews
as entrypoint.
This method creates a set of NSLayoutConstraint, UILayoutGuide, and modifiers of UIView.
Finally, those apply. You don't need to call addSubview
. that goes automatically according to hierarchy from layout descriptions.
Examples
Sample code assumes run in UIView
. (self is UIView
)
You can replace it with UIViewController.view
.
Layout subviews inside safe-area
Attaching to top and bottom safe-area.
Put a view snapping to edge
synonyms:
Add constraints to view itself
Stacking views on Z axis
relative(0)
fills to the edges of ZStackBlock
.
Centering a label with minimum padding
Detail
Vertically and Horizontally Stack layout
VStackBlock
Alignment
center(default) | leading | trailing | fill |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
HStackBlock
center(default) | top | bottom | fill |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
Background modifier

Overlay modifier
Relative modifier
.relative
modifier describes that the content attaches to specified edges with padding.
Not specified edges do not have constraints to the edge. so the sizing depends on intrinsic content size.
You might use this modifier to pin to edge as an overlay content.
Padding modifier
.padding
modifier is similar with .relative
but something different.
Different with that, Not specified edges pin to edge with 0 padding.
ZStackBlock
![]() |
![]() |
Stacking views in Z axis (aligns in center)
Expands to specified edges each view
Specifying alignment each view
Animations
// TODO:
https://github.com/muukii/MondrianLayout/issues/19
Classic Layout API
Structured layout API(DSL) does not cover the all of use-cases.
Sometimes we still need a way to describe constraints for a complicated layout.
MondrianLayout provides it as well other AutoLayout libraries.
Activate constraints independently
Batch layout**
Examples
Attach in horizontally
Attach in vertically
Edge attaches to other edge
Installation
CocoaPods
SwiftPM
LICENSE
MondrianLayout is released under the MIT license.