🌟 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.
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.
class MyView: UIView {
let nameLabel: UILabel
let detailLabel: UILabel
init() {
super.init(frame: .zero)
// Seting up constraints constraints, layoutGuides and adding subviews
Mondrian.buildSubviews(on: self) {
VStackBlock {
nameLabel
detailLabel
}
}
// Seting up constraints for the view itself.
Mondrian.layout {
self.mondrian.layout.width(200) // can be method cain.
}
}
}
Examples
Sample code assumes run in UIView. (self is UIView) You can replace it with UIViewController.view.
label
.viewBlock // To enable view describes layout
.padding(8)
.background(backgroundView)
Overlay modifier
label
.viewBlock // To enable view describes layout
.padding(8)
.overlay(overlayView)
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.
MondrianLayout
A way to build AutoLayout rapidly than using InterfaceBuilder(XIB, Storyboard) in iOS.
Describing the layout ergonomically in the code
🏗 Structured Layout API
💃 To preserve flexibility of AutoLayout, we have classical style API
Classical Layout API
Support the project
This image laid out by MondrianLayout
Layout code
Structured layout API and Classical layout API
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
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 isUIView
)You can replace it with
UIViewController.view
.Layout subviews inside safe-area
Attaching to top and bottom safe-area.
or
Put a view snapping to edge
synonyms:
Add constraints to view itself - using classic layout
or
Stacking views on Z axis
relative(0)
fills to the edges ofZStackBlock
.Centering a label with minimum padding
Detail
Vertically and Horizontally Stack layout
VStackBlock
Alignment | center(default) | leading | trailing | fill | |—|—|—|—| |
|
|
|
|
HStackBlock
StackingSpacer
Adds a space in stacking layout block.
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
Updating layout dynamically
LayoutManager
does support it.If we need to change the layout each some conditions such as depending traits, this object helps that.
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.