Native drag-and-drop for SwiftUI lists on iOS, powered by UIDragInteraction.
- Flat lists (
SwiftDragView) - Sectioned lists with cross-section dragging (
SwiftDragSectionedView) - Native iOS drag previews and animations
- Haptic feedback (respects Reduce Motion)
- Configurable corner radius, restore delay, and UTTypes
- iOS 16+
- Swift 5.9+
https://github.com/iamjoaovytor/SwiftDrag.git
Or add to your Package.swift:
dependencies: [
.package(url: "https://github.com/iamjoaovytor/SwiftDrag.git", from: "1.0.0")
]| Flat List | Sectioned List |
|---|---|
![]() |
![]() |
import SwiftDrag
struct Todo: SwiftDragItem {
let id: UUID
var title: String
}That's it. isDraggable defaults to true and getItemProvider() returns an empty provider.
@State private var todos: [Todo] = [
Todo(id: UUID(), title: "Buy groceries"),
Todo(id: UUID(), title: "Walk the dog"),
Todo(id: UUID(), title: "Read a book"),
]
var body: some View {
ScrollView {
LazyVStack(spacing: 8) {
SwiftDragView(items: $todos) { todo in
// Your card view
HStack {
Image(systemName: "line.3.horizontal")
.foregroundStyle(.tertiary)
Text(todo.title)
Spacer()
}
.padding()
.background(Color(.secondarySystemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 10))
} dropView: { _ in
// Placeholder shown while dragging
RoundedRectangle(cornerRadius: 10)
.strokeBorder(style: StrokeStyle(lineWidth: 2, dash: [6, 4]))
.foregroundStyle(.quaternary)
.frame(height: 50)
} dropCompleted: {
print("Reordered!")
}
}
.padding()
}
}struct TodoSection: SwiftDragSection {
let id: UUID
var title: String
var items: [Todo]
}
@State private var sections: [TodoSection] = [
TodoSection(id: UUID(), title: "To Do", items: [
Todo(id: UUID(), title: "Setup project"),
Todo(id: UUID(), title: "Write tests"),
]),
TodoSection(id: UUID(), title: "Done", items: [
Todo(id: UUID(), title: "Define architecture"),
]),
]
var body: some View {
ScrollView {
LazyVStack(spacing: 0) {
SwiftDragSectionedView(sections: $sections) { section in
// Section header
Text(section.title)
.font(.headline)
.padding()
} card: { todo in
// Item card
Text(todo.title)
.padding()
.background(Color(.secondarySystemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 8))
.padding(.horizontal)
.padding(.bottom, 6)
} dropView: { _ in
RoundedRectangle(cornerRadius: 8)
.strokeBorder(style: StrokeStyle(lineWidth: 2, dash: [6, 4]))
.foregroundStyle(.quaternary)
.frame(height: 40)
.padding(.horizontal)
} dropCompleted: {
print("Sections updated!")
}
}
}
}Check out Examples/SwiftDragExample.swift for a ready-to-run demo with both flat and sectioned lists. Just copy it into your project.
// Drag preview corner radius (default: 12)
.environment(\.dragPreviewCornerRadius, 16)
// Delay before restoring the original view after lift (default: 0.2s)
.environment(\.dragRestoreDelay, 0.3)
// UTTypes for external drop support (default: [] = internal only)
.environment(\.dragSupportedUTTypes, [.text])struct Todo: SwiftDragItem {
let id: UUID
var title: String
var isDraggable: Bool = true // set to false to lock an item
}MIT

