dragConfiguration

Configures a drag session.

View ModifieriOS 27.0+macOS 26.0+visionOS 27.0+dragConfiguration(_:) in SwiftUI

Declaration

React
compose().dragConfiguration(
  configuration: SwiftUI.DragConfiguration
)
Swift
nonisolated func dragConfiguration(
    _ configuration: DragConfiguration
) -> some View

Parameters

Return value

A view that configures a drag session in a way, described

Discussion

Below is a simplified example of a view that supports copy, move and delete operations for drag.

Drag to delete into trash bin

If a view wants to support drag-to-delete into the trash bin or another location that has similar semantics, it should specify the support for this operation in a drag configuration:

    @State private var photos: [Photo] = []
    @State private var selectedPhotos: [Photo.ID] = []

    var body: some View {
        ScrollView {
            LazyVGrid(columns: gridColumns) {
                ForEach(photos) { photo in
                    PhotoView(photo: photo)
                        .draggable(containerItemID: photo.id)
                }
            }
        }
        .dragContainer(for: Photo.self) { draggedIDs in
            photos(ids: draggedIDs)
        }
        .dragContainerSelection(selectedPhotos)
        .dragConfiguration(DragConfiguration(allowMove: false, allowDelete: true))
        .onDragSessionUpdated { session in
            if session.phase == .ended(.delete) {
                let ids = session.draggedItemIDs(for: Photo.ID.self)
                removeAndTrash(ids)
            }
        }
        .dragPreviewsFormation(.stack)
    }

    func removeAndTrash(_ ids: [Photo.ID]) {
        ids.forEach { id
            if let idx = photos.firstIndex(where: { $0.id == id }) {
                let photo = photos[idx]
                photos.remove(at: idx)
                try? FileManager.default.trashItem(
                    at: photo.fileURL, resultingItemURL: nil
                )
            }
        }
    }
}

Note, that any drag supports copy operation by default. In the snippet above, the view supports both copy and delete operations.

of a drag session. by the configuration parameter.

See also

More in Gestures and input.