Graphics Reference
In-Depth Information
Figure 2.1
An image displayed inside the backing layer of a
UIView
That was some very simple code, and yet we've done something quite interesting here:
Using the power of
CALayer
, we've displayed an image inside an ordinary
UIView
. This
isn't a
UIImageView
; it's not designed to display images normally. By manipulating the
layer directly, we've exposed new functionality and made our humble
UIView
a lot more
interesting.
contentsGravity
You might have noticed that our snowman looks a bit…
fat.
The image we loaded wasn't
precisely square, but it's been stretched to fit the view. You've probably seen a similar
situation when using
UIImageView
, and the solution there would be to set the
contentMode
property of the view to something more appropriate, like this:
view
.
contentMode
= UIViewContentModeScaleAspectFit;
That approach works here as well (give it a try), but most visual properties of
UIView
—
such as
contentMode
—are really just manipulating equivalent properties of the
underlying layer.
The equivalent property of
CALayer
is called
contentsGravity
, and it is an
NSString
rather than an
enum
like its UIKit counterpart. The
contentsGravity
string should be set to one of the following constant values: