Graphics Reference
In-Depth Information
You might have come across the
UIViewAutoresizingMask
constants, used to
control how a
UIView
frame
is updated when its superview changes size (usually in
response to the screen rotating from landscape to portrait or vice versa).
In iOS 6, Apple introduced the
autolayout
mechanism. This works in a different and more
sophisticated way than the autoresizing mask, by specifying
constraints
that combine to
form a system of linear equations and inequalities that define the position and size of your
views.
On Mac OS,
CALayer
has a property called
layoutManager
that enables you to utilize
these automatic layout mechanisms using the
CALayoutManager
informal protocol and
the
CAConstraintLayoutManager
class. For some reason, however, these are not
available on iOS.
When using layer-backed views, you can make use of the
UIViewAutoresizingMask
and
NSLayoutConstraint
APIs exposed by the
UIView
class interface. But if you
want to control the layout of an arbitrary
CALayer
, you need to do it manually. The
simplest way to do that is using the following method of the
CALayerDelegate
:
- (
void
)layoutSublayersOfLayer:(
CALayer
*)layer;
This method is called automatically whenever the layer
bounds
changes or the
-setNeedsLayout
method is called on the layer. It gives you the opportunity to
reposition and resize your sublayers programmatically, but offers no automatic default
behaviors for keeping layers aligned after a screen rotation like the
UIView
autoresizingMask
and
constraints
properties.
This is another good reason to try to construct your interfaces using views whenever
possible rather than using hosted layers.
This chapter covered the geometry of
CALayer
, including its
frame
,
position
, and
bounds
, and we touched on the concept of layers existing in three-dimensional space
instead of a flat plane. We also discussed how touch handing can be implemented when
working with hosted layers, and the lack of support for autoresizing and autolayout in Core
Animation on iOS.
In Chapter 4, “Visual Effects,” we explore some of Core Animation's layer appearance
features.