WeView 2

An alternative to iOS Auto Layout

Tutorial 4: Basics

WeView 2 provides a container (The WeView class) that offers a variety of layouts for its subviews.

Normally, when working with UIKit there is only the superview and its subviews, and subviews are added directly to their superview using [UIView addSubview:].

WeView 2 introduces layouts, which a WeView superview uses to lay out (ie. measure and arrange) its subviews. We add subviews to a WeView using a method that specifies the layout to apply to those subviews.

Typically, we only configure layout then, as subviews are added to a WeView.

A Simple Example

Here is an example WeView that has three subviews: a UILabel, a UIImageView and a UIButton.

When & How Does Layout Occur?

Most importantly, layout is automated. In most cases, you don’t need any more code than shown in the example above. Layout is triggered automatically.

UIKit provides a mechanism for triggering layout: UIView’s needsLayout property. It is a simple “dirty flag” for layout. UIKit ensures that [UIView layoutSubviews] is eventually called whenever needsLayout is set. Layout is needed in the following conditions:

WeView 2 automatically triggers layout in all but the last of these conditions. In that case, you should trigger layout yourself by setting the needsLayout property on the WeView to YES.

Once you’ve added a subview to a WeView with a layout, you should never try to manually change its position or size. The WeView will usually overrule any changes you make to the subview.