<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.12.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "QuartzCore",
  "identifier" : "/documentation/QuartzCore/CALayerDelegate/draw(_:in:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core Animation"
    ],
    "preciseIdentifier" : "c:objc(pl)CALayerDelegate(im)drawLayer:inContext:"
  },
  "title" : "draw(_:in:)"
}
-->

# draw(_:in:)

Tells the delegate to implement the display process using the layer’s context.

```
optional func draw(_ layer: CALayer, in ctx: CGContext)
```

## Parameters

`layer`

The layer whose contents need to be drawn.

`ctx`

The graphics context to use for drawing. The graphics context incorporates the appropriate scale factor for drawing to the target screen.

## Discussion

The [`draw(_:in:)`](/documentation/QuartzCore/CALayerDelegate/draw(_:in:)) method is called when the layer is marked for its content to be reloaded, typically with the [`setNeedsDisplay()`](/documentation/QuartzCore/CALayer/setNeedsDisplay()) method. It is not called if the delegate implements the [`display(_:)`](/documentation/QuartzCore/CALayerDelegate/display(_:)) method. You can use the context to draw vectors, such as curves and lines, or images with the <doc://com.apple.documentation/documentation/CoreGraphics/CGContext/draw(_:in:byTiling:)> method.

The following code shows how you can create a class named `LayerDelegate` that implements [`CALayerDelegate`](/documentation/QuartzCore/CALayerDelegate) and sets it as a layer’s (named `sublayer`) delegate. When [`setNeedsDisplay()`](/documentation/QuartzCore/CALayer/setNeedsDisplay()) is called on `sublayer`, the delegate’s [`draw(_:in:)`](/documentation/QuartzCore/CALayerDelegate/draw(_:in:)) method draws an ellipse fitting the bounding box of the layer using the <doc://com.apple.documentation/documentation/CoreGraphics/CGContext/boundingBoxOfClipPath> function.

```swift
let delegate = LayerDelegate()
    
lazy var sublayer: CALayer = {
    let layer = CALayer()
    
    layer.delegate = self.delegate
    
    return layer
}()
    
// sublayer.setNeedsDisplay()
    
class LayerDelegate: NSObject, CALayerDelegate {
    func draw(_ layer: CALayer, in ctx: CGContext) {
        ctx.addEllipse(in: ctx.boundingBoxOfClipPath)
        ctx.strokePath()
    }
}
```

> Important:
> This method is not called if the delegate implements ``doc://com.apple.quartzcore/documentation/QuartzCore/CALayerDelegate/display(_:)``.

---

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)