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

# addTrackingRect(_:owner:userData:assumeInside:)

Establishes an area for tracking mouse-entered and mouse-exited events within the view and returns a tag that identifies the tracking rectangle.

```
func addTrackingRect(_ rect: NSRect, owner: Any, userData data: UnsafeMutableRawPointer?, assumeInside flag: Bool) -> NSView.TrackingRectTag
```

## Parameters

`rect`

A rectangle that defines a region of the view for tracking mouse-entered and mouse-exited events.

`owner`

The object that gets sent the event messages. It can be the view itself or some other object (such as an NSCursor or a custom drawing tool object), as long as it responds to both [`mouseEntered(with:)`](/documentation/AppKit/NSResponder/mouseEntered(with:)) and [`mouseExited(with:)`](/documentation/AppKit/NSResponder/mouseExited(with:)).

`data`

Data stored in the [`NSEvent`](/documentation/AppKit/NSEvent) object for each tracking event.

`flag`

If <doc://com.apple.documentation/documentation/Swift/true>, the first event will be generated when the cursor leaves `aRect`, regardless if the cursor is inside `aRect` when the tracking rectangle is added. If <doc://com.apple.documentation/documentation/Swift/false> the first event will be generated when the cursor leaves `aRect` if the cursor is initially inside `aRect`, or when the cursor enters `aRect` if the cursor is initially outside `aRect`.  You usually want to set this flag to <doc://com.apple.documentation/documentation/Swift/false>.

## Return Value

A tag that identifies the tracking rectangle. It is stored in the associated [`NSEvent`](/documentation/AppKit/NSEvent) objects and can be used to remove the tracking rectangle.

## Discussion

Tracking rectangles provide a general mechanism that can be used to trigger actions based on the cursor location (for example, a status bar or hint field that provides information on the item the cursor lies over). To simply change the cursor over a particular area, use [`addCursorRect(_:cursor:)`](/documentation/AppKit/NSView/addCursorRect(_:cursor:)). If you must use tracking rectangles to change the cursor, the [`NSCursor`](/documentation/AppKit/NSCursor) class specification describes the additional methods that must be invoked to change cursors by using tracking rectangles.

In macOS 10.5 and later, tracking areas provide a greater range of functionality (see [`addTrackingArea(_:)`](/documentation/AppKit/NSView/addTrackingArea(_:))).

## See Also

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

Adds a given tracking area to the view.

[`userData`](/documentation/AppKit/NSEvent/userData)

The data associated with a mouse-tracking event.



---

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)