An alternative to iOS Auto Layout
What determines how a WeView 2 layout will size a subview?
The [UIView sizeThatFits:(CGSize)size] method returns the desired size for any given view. A view’s desired size reflects the “natural” size for its content.
The desired size of a non-wrapping UILabel is determined by its text, font, etc.
The desired size of a UIImageView is the size of its image.
The desired size of a WeView is the minimum size in which its subviews can be layed out properly, honoring its margins, spacing, etc.
If a WeView has multiple layouts, its minimum size is the max width or height needed by any of its layouts.
For some UIViews, their desired size is context-dependent.
A UILabel can wrap its text (for example, if numberOfLines = 0 and lineBreakMode is NSLineBreakByWordWrapping). For a wrapping UILabel, it’s desired size depends on the available space.
The less horizontal space is available, the greater will be the UILabel’s desired height.
Similary, WeViews that use a Flow Layout wrap their contents like text.
Because desired size may depend on the layout context, the [UIView sizeThatFits:(CGSize)size] method has a size parameter that reflects the available space. The width and height of the size parameter should be >= 0.
If [UIView sizeThatFits:(CGSize)size] is called with a size of CGSizeZero (ie. width = 0 and height = 0), a UIView should return its ideal desired size. This is its desired size in the abstract, outside of any specific layout. For a UILabel, this would be the desired size without any text wrap.
However, if [UIView sizeThatFits:(CGSize)size] is called with a non-zero size, the UIView should return its desired size in the context of the available space.
All built-in UIKit views (ie. UILabel, UIButton) properly report their desired size using [UIView sizeThatFits:]. Custom UIViews should also implement that method in order to work properly with WeView 2, although you can also manipulate their desired size using methods like [UIView setFixedSize:] (see below).
WeView 2 offer a variety of ways to manipulate the desired size of a subview, without subclassing it or otherwise modifying its implementation of [UIView sizeThatFits:(CGSize)size].
- (UIView *)setMinDesiredWidth:(CGFloat)value;
- (UIView *)setMaxDesiredWidth:(CGFloat)value;
- (UIView *)setMinDesiredHeight:(CGFloat)value;
- (UIView *)setMaxDesiredHeight:(CGFloat)value;
These methods let you set a maximum or minimum desired width or height. By setting both to the same value you can fix the desired width or height to that value.
- (UIView *)setFixedDesiredWidth:(CGFloat)value;
- (UIView *)setFixedDesiredHeight:(CGFloat)value;
- (UIView *)setFixedDesiredSize:(CGSize)value;
These methods let you set more than one property at a time.
You usually won’t need these methods.
- (UIView *)setDesiredWidthAdjustment:(CGFloat)value;
- (UIView *)setDesiredHeightAdjustment:(CGFloat)value;
- (UIView *)setDesiredSizeAdjustment:(CGSize)value;
Additionally, you can adjust the desired width or height of a subview using these methods relative to the desired size it reports. [setDesiredWidthAdjustment:+10.f], for example, means: “Treat this subview as though it wanted to be 10 points wider than it think it should be”.
The desired size adjustment properties can have positive and negative values.
- (UIView *)setIgnoreDesiredSize:(BOOL)value;
// Equivalent to _\[UIVIew setIgnoreDesiredSize:YES\]_.
- (UIView *)setIgnoreDesiredSize;
If this property is set, the WeView layouts will treat the desired size of this subview as CGSizeZero, ie. width = 0, height = 0. We usually want to ignore the desired size of a subview when the subview is set to stretch horizontally and vertically.
Next: Tutorial 11: Stretch