Draws the receiver’s image within the passed-in rectangle.
- iOS 2.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
The portion of the view’s bounds that needs to be updated. The first time your view is drawn, this rectangle is typically the entire visible bounds of your view. However, during subsequent drawing operations, the rectangle may specify only part of your view.
The default implementation of this method does nothing. Subclasses that use technologies such as Core Graphics and UIKit to draw their view’s content should override this method and implement their drawing code there. You do not need to override this method if your view sets its content in other ways. For example, you do not need to override this method if your view just displays a background color or if your view sets its content directly using the underlying layer object.
By the time this method is called, UIKit has configured the drawing environment appropriately for your view and you can simply call whatever drawing methods and functions you need to render your content. Specifically, UIKit creates and configures a graphics context for drawing and adjusts the transform of that context so that its origin matches the origin of your view’s bounds rectangle. You can get a reference to the graphics context using the
UIGraphics function, but do not establish a strong reference to the graphics context because it can change between calls to the
Similarly, if you draw using OpenGL ES and the
GLKView class, GLKit configures the underlying OpenGL ES context appropriately for your view before calling this method (or the
glk method of your
GLKView delegate), so you can simply issue whatever OpenGL ES commands you need to render your content. For more information about how to draw using OpenGL ES, see OpenGL ES Programming Guide.
You should limit any drawing to the rectangle specified in the
rect parameter. In addition, if the
is property of your view is set to
draw(_:) method must totally fill the specified rectangle with opaque content.
If you subclass
UIView directly, your implementation of this method does not need to call
super. However, if you are subclassing a different view class, you should call
super at some point in your implementation.
This method is called when a view is first displayed or when an event occurs that invalidates a visible part of the view. You should never call this method directly yourself. To invalidate part of your view, and thus cause that portion to be redrawn, call the
set method instead.