WeView 2

An alternative to iOS Auto Layout

Tutorial 5: Layout Model

WeView uses a cell-based layout model. Layouts arrange their subviews by dividing the superview’s bounds into cells.

The layout process works like this:

An Example

Here is a horizontal layout with three UILabels:

I’ve assigned a background color to each UILabel so that its frame (ie. size and position) is clear.

Each subview has its own cell in the layout. In the animation above, the subviews are stretched to fill their cells to emphasize the difference between a subview and its cell.

Alignment

In our first example, the WeView was shown at its desired size, ie. just large enough to exactly fit its subviews. Let’s see what happens if that WeView is larger than its desired size.

Layout Alignment vs. Cell Alignment

Here’s the same WeView if we use top and right alignment.

Note that the layout cells are top- and right-aligned within the superview’s bounds and the subviews are top- and right-aligned within their layout cells. The alignment of the subviews within their cells (or cell alignment) defaults to the layout’s alignment unless we set an explicit cell alignment for a given subview.

We might want the labels to be top-aligned as a group within their superview (ie. the alignment property of the layout) but to be bottom-aligned with respect to each other (ie. the cellAlignment property of each subview).

Custom Cell Alignment

Here we use center vertical cell alignment.