<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "tvOS: -",
    "visionOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIView/draw(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIView(im)drawRect:"
  },
  "title" : "draw(_:)"
}
-->

# draw(_:)

Draws the view’s image within the passed-in rectangle.

```
func draw(_ rect: CGRect)
```

## Parameters

`rect`

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.

## Discussion

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 don’t need to override this method if your view sets its content in other ways. For example, you don’t 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 [`UIGraphicsGetCurrentContext()`](/documentation/UIKit/UIGraphicsGetCurrentContext()) function, but don’t establish a strong reference to the graphics context because it can change between calls to the [`draw(_:)`](/documentation/UIKit/UIView/draw(_:)) method.

Similarly, if you draw using OpenGL ES and the <doc://com.apple.documentation/documentation/GLKit/GLKView> class, GLKit configures the underlying OpenGL ES context appropriately for your view before calling this method (or the <doc://com.apple.documentation/documentation/GLKit/GLKViewDelegate/glkView(_:drawIn:)> method of your <doc://com.apple.documentation/documentation/GLKit/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](https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008793).

You should limit any drawing to the rectangle specified in the `rect` parameter. In addition, if the [`isOpaque`](/documentation/UIKit/UIView/isOpaque) property of your view is set to <doc://com.apple.documentation/documentation/Swift/true>, your [`draw(_:)`](/documentation/UIKit/UIView/draw(_:)) method must totally fill the specified rectangle with opaque content.

If you subclass [`UIView`](/documentation/UIKit/UIView) directly, your implementation of this method doesn’t need to call `super`. However, if you’re 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 [`setNeedsDisplay()`](/documentation/UIKit/UIView/setNeedsDisplay()) or [`setNeedsDisplay(_:)`](/documentation/UIKit/UIView/setNeedsDisplay(_:)) method instead.

In iOS 18 and later, UIKit supports automatic trait tracking inside this method for traits from this view’s `traitCollection`. For more information, see [Automatic trait tracking](/documentation/UIKit/automatic-trait-tracking).

This method supports automatic observation tracking. For more information, see [Updating views automatically with observation tracking in UIKit](/documentation/UIKit/updating-views-automatically-with-observation-tracking-in-uikit).

## See Also

[`contentMode`](/documentation/UIKit/UIView/contentMode-swift.property)

A flag used to determine how a view lays out its content when its bounds change.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)