Chapter 2. Drawing
The views illustrated in Chapter 1 were mostly colored rectangles; they had a backgroundColor
and no more. But that’s not what a real iOS program looks like. Everything the user sees is a UIView, and what the user sees is a lot more than a bunch of colored rectangles. That’s because the views that the user sees have content. They contain drawing.
Many UIView subclasses, such as a UIButton or a UILabel, know how to draw themselves. Sooner or later, you’re also going to want to do some drawing of your own. You can prepare your drawing as an image file beforehand. You can draw an image as your app runs, in code. You can display an image in a UIView subclass that knows how to show an image, such as a UIImageView or a UIButton. A pure UIView is all about drawing, and it leaves that drawing largely up to you; your code determines what the view draws, and hence what it looks like in your interface.
This chapter discusses the mechanics of drawing. Don’t be afraid to write drawing code of your own! It isn’t difficult, and it’s often the best way to make your app look the way you want it to. (I’ll discuss how to draw text in Chapter 11.)
Images and Image Views
The basic general UIKit image class is UIImage. UIImage knows how to deal with many standard image types, such as HEIC, TIFF, JPEG, GIF, and PNG. A UIImage can be used wherever an image is to be displayed; it knows how to provide the image data, and may be thought of loosely as wrapping the image data. It also ...
Get Programming iOS 14 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.