<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSView/draw(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSView(im)drawRect:"
  },
  "title" : "draw(_:)"
}
-->

# draw(_:)

Overridden by subclasses to draw the view’s image within the specified rectangle.

```
func draw(_ dirtyRect: NSRect)
```

## Parameters

`dirtyRect`

A rectangle defining the portion of the view that requires redrawing. This rectangle usually represents the portion of the view that requires updating. When responsive scrolling is enabled, this rectangle can also represent a nonvisible portion of the view that AppKit wants to cache.

## Discussion

Use this method to draw the specified portion of your view’s content. Your implementation of this method should be as fast as possible and do as little work as possible. The `dirtyRect` parameter helps you achieve better performance by specifying the portion of the view that needs to be drawn. You should always limit drawing to the content inside this rectangle. For even better performance, you can call the [`getRectsBeingDrawn(_:count:)`](/documentation/AppKit/NSView/getRectsBeingDrawn(_:count:)) method and use the list of rectangles returned by that method to limit drawing even further. You can also use the [`needsToDraw(_:)`](/documentation/AppKit/NSView/needsToDraw(_:)) method to test whether objects in a particular rectangle need to be drawn.

The default implementation does nothing. Subclasses should override this method if they do custom drawing. Prior to calling this method, AppKit creates an appropriate drawing context and configures it for drawing to the view; you do not need to configure the drawing context yourself. If your app manages content using its layer object instead, use [`updateLayer()`](/documentation/AppKit/NSView/updateLayer()) to update your layer instead of overriding this method.

If your custom view is a direct `NSView` subclass, you do not need to call `super`. For all other views, call `super` at some point in your implementation so that the parent class can perform any additional drawing.

> Important:
> If the view’s ``doc://com.apple.appkit/documentation/AppKit/NSView/isOpaque`` property is <doc://com.apple.documentation/documentation/Swift/true>, the view must completely fill the `dirtyRect` rectangle with opaque content.

For more information, see [Drawing](/documentation/AppKit/nsview-drawing).

This method also supports automatic observation tracking. See [Updating views automatically with observation tracking in AppKit](/documentation/AppKit/updating-views-automatically-with-observation-tracking-in-appkit).

## See Also

[`shouldDrawColor()`](/documentation/AppKit/NSView/shouldDrawColor())

Returns a Boolean value indicating whether the view is being drawn to an environment that supports color.

[`setNeedsDisplay(_:)`](/documentation/AppKit/NSView/setNeedsDisplay(_:))

Marks the region of the view within the specified rectangle as needing display, increasing the view’s existing invalid region to include it.

[`display()`](/documentation/AppKit/NSView/display())

Displays the view and all its subviews if possible, invoking each of the `NSView` methods [`lockFocus()`](/documentation/AppKit/NSView/lockFocus()), [`draw(_:)`](/documentation/AppKit/NSView/draw(_:)), and [`unlockFocus()`](/documentation/AppKit/NSView/unlockFocus()) as necessary.

[`isFlipped`](/documentation/AppKit/NSView/isFlipped)

A Boolean value indicating whether the view uses a flipped coordinate system.



---

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)