<!--
{
  "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:itemID:in:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE13dragContainer3for6itemID2in_Qrqd_0_m_s7KeyPathCyqd_0_qd__GAA9NamespaceV0H0VSgqd_1_Sayqd__GctSHRd__s8SendableRd__16CoreTransferable0O0Rd_0_7ElementQyd_1_Rsd_0_SlRd_1_r1_lF::OverloadGroup"
  },
  "title" : "dragContainer(for:itemID:in:_:)"
}
-->

# dragContainer(for:itemID:in:_:)

A container with draggable views.

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

## Parameters

`itemType`

A type of the dragged items.

`itemID`

A closure that provides an item’s identifier.

`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. Using 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

In an example below, an app presents a view with `Fruit` values.
`Fruit` does not conform to `Identifiable` but uses its name
as its identifier.

```
@State private var fruits: [Fruit]
@State private var selection: [String]

var body: some View {
    VStack {
        ForEach(fruits) { fruit in
            FruitView(fruit)
                .draggable(containerItemID: fruit.name)
        }
    }
    .dragContainer(itemID: \Fruit.name) { ids in
       fruits(with: ids)
    }
}

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

struct Fruit: Transferable {
    var name: String
    ...
}
```

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)