<!--
{
  "availability" : [
    "iOS: 13.4.0 -",
    "iPadOS: 13.4.0 -",
    "macCatalyst: 13.4.0 -",
    "macOS: 10.15.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/onDrag(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE6onDragyQrSo14NSItemProviderCycF"
  },
  "title" : "onDrag(_:)"
}
-->

# onDrag(_:)

Activates this view as the source of a drag and drop operation.

```
nonisolated func onDrag(_ data: @escaping () -> NSItemProvider) -> some View
```

## Parameters

`data`

A closure that returns a single
<doc://com.apple.documentation/documentation/Foundation/NSItemProvider> that
represents the draggable data from this view.

## Return Value

A view that activates this view as the source of a drag and
drop operation, beginning with user gesture input.

## Discussion

Applying the `onDrag(_:)` modifier adds the appropriate gestures for
drag and drop to this view. When a drag operation begins, a rendering of
this view is generated and used as the preview image.

To customize the default preview, apply a
[`contentShape(_:_:eoFill:)`](/documentation/SwiftUI/View/contentShape(_:_:eoFill:)) with a
[`dragPreview`](/documentation/SwiftUI/ContentShapeKinds/dragPreview) kind. For example, you can change the
preview’s corner radius or use a nested view as the preview.

If you want to show a different preview, you can use
[`onDrag(_:preview:)`](/documentation/SwiftUI/View/onDrag(_:preview:)).

To use `Transferable` types directly and access a richer feature
set — including multi-item drag via
[`dragContainer(for:in:_:)`](/documentation/SwiftUI/View/dragContainer(for:in:_:)) — use [`draggable(_:)`](/documentation/SwiftUI/View/draggable(_:))
instead.

> Important: Don’t dispatch work to the main actor while exporting the
> item. Doing so isn’t supported and might cause a hang. If you register
> data representations on the item provider yourself, make sure
> their completion handlers don’t dispatch to the main thread.

---

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)