Skip to content
Markus Riegel edited this page Sep 19, 2017 · 1 revision

How Baya works.

Baya encapsulates layout logic in BayaLayouts. You can apply this logic to UIViews or other BayaLayouts by wrapping them with a BayaLayout. The simplest way to do this is by using extensions functions like layoutGravitating(horizontally:vertically).

Layout definition and execution are spearated. Typically you would define your layout once, e.g. in the loadView(), save the root BayaLayout in a variable and execute it each time the frame or the content changes.

Measurement and layout.

Before positioning its elements each BayaLayout makes a measure pass. This is done by calling sizeThatFits(_:) on its element and caching the values. The layout pass uses these values to position the elments.

Using startLayout(with:) will start the measurement pass, starting from the root layout and going deeper. After each BayaLayoutable in the layout tree has been mesaure, the layout pass is started with layoutWith(frame:). You can also kick off these procedures on their own. E.g. calling startMeasure(with:) is needed when using UICollectionViewCells.

Layout margins.

Baya takes layout margins into account by default. Most BayaLayouts also have an additional parameter which you can use to set the margins of that layout.

Match parent and wrap content.

Each BayaLayout takes into account the layout mode of its element(s) when positioning and sizing them. The default Modes for a BayaLayoutable are wrapContent on both axis, meaning its parent will try do give it the size that was measured.

You can override the default behavior by using layoutMatchingParent(). When the Mode of an axis is matchParent, the parent BayaLayout will try to us the available size instead of the measured one.

Intro

Wiki

Clone this wiki locally