-
Notifications
You must be signed in to change notification settings - Fork 1
Basics
Baya encapsulates layout logic in BayaLayouts
. You can apply this logic to UIView
s 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.
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 UICollectionViewCell
s.
Baya takes layout margins into account by default. Most BayaLayout
s also have an additional parameter which you can use to set the margins of that layout.
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.