onDrag

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

View ModifieriOS 13.4+macOS 10.15+

Overloads

2 ways to call compose().onDrag(). Each is one SwiftUI overload, picked by the arguments you pass.

React
compose().onDrag(data: string)
Swift
nonisolated func onDrag(
    _ data: @escaping () -> NSItemProvider
) -> some View

Parameters

  • data*string

    A closure that returns a single

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 View.contentShape(_:_:eoFill:) with a 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 View.onDrag(_:preview:).

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

doc://com.apple.documentation/documentation/Foundation/NSItemProvider that represents the draggable data from this view.

See also

More in Events and lifecycle.