<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 26.0.0 -",
    "visionOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/dragContainer(for:in:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE13dragContainer3for2in_Qrqd__m_AA9NamespaceV2IDVSgqd_0_SayAIs12IdentifiablePQyd__Gct16CoreTransferable0L0Rd__sALRd__7ElementQyd_0_Rsd__SlRd_0_s8SendableANRQr0_lF::OverloadGroup"
  },
  "title" : "dragContainer(for:in:_:)"
}
-->

# dragContainer(for:in:_:)

A container with draggable views where the drag payload is based
on multiple identifiers of dragged items.

```
nonisolated func dragContainer<Item, Data>(for itemType: Item.Type = Item.self, in namespace: Namespace.ID? = nil, _ payload: @escaping (Array<Item.ID>) -> Data) -> some View where Item : Transferable, Item : Identifiable, Item == Data.Element, Data : Collection, Item.ID : Sendable
```

## Parameters

`itemType`

A type of the dragged items.

`namespace`

A namespace that identifies the drag container.

`payload`

A closure which is called when
a drag operation begins. As an argument, the closure receives either the identifiers
of all the selected items, if the dragged item is a part of selection
or only the identifier of the dragged item, if it is
not part of the selection. With the passed identifiers, put together the payload to drag,
and return from the closure.
Return an empty `Collection` to disable the drag.

## Return Value

A view that can be activated as the source of a drag and
drop operation, beginning with user gesture input.

## Discussion

Provide the selected identifiers list to SwiftUI using
`dragContainerSelection(_:containerNamespace)` modifier.
In a case when there’s no selection information available,
SwiftUI passes the dragged item identifier to the `payload` closure.

In an example below, an app presents a view with `Fruit` values.
When a user starts drag, SwiftUI uses the selection to put together
the list of item identifiers to drag.

```
var fruits: [Fruit]
@State private var selection: [Fruit.ID]

var body: some View {
    VStack {
        ForEach(fruits) { fruit in
            FruitView(fruit)
                .draggable(containerItemID: fruit.id)
        }
    }
    .dragContainer(for: Fruit.self) { ids in
       fruits(with: ids)
    }
    .dragContainerSelection(selection)
}

func fruits(with ids: [UUID]) -> [Fruit] { ... }

struct Fruit: Transferable, Identifiable { ... }
```

To enable multi-item drag, apply this modifier to a container
view and mark each draggable child with [`draggable(_:)`](/documentation/SwiftUI/View/draggable(_:))
or [`draggable(containerItemID:containerNamespace:)`](/documentation/SwiftUI/View/draggable(containerItemID:containerNamespace:)).

---

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)