reorderContainer(for:in:isEnabled:move:)
Defines a container that allows its items to be reordered.
Declaration
nonisolated func reorderContainer<Item, CollectionID>(for item: Item.Type, in collectionID: CollectionID.Type, isEnabled: Bool = true, move: @escaping (ReorderDifference<Item.ID, CollectionID>) -> ()) -> some View where Item : Identifiable, CollectionID : Hashable, CollectionID : Sendable, Item.ID : Sendable
Mentioned in
Discussion
Declare this modifier on your container or layout view to make it a reorderable container. Then, apply reorderable() to the content of your container to make those items reorderable.
Use this overload if your container has multiple collections in it. If your container only has a single collection, use the convenience reorderContainer(for:isEnabled:move:) modifier.
A reorderable item within the container can be lifted using a drag gesture. As that item lifts, a placeholder view will take its place to indicate where the moved view will be when dropped. As your user moves the item through the container, the position of the placeholder will update to be the last item that your user dragged over. When they drop the item, the move closure will be called with the change provided.
The change is provided as a difference to the closure. The difference contains the identifiers of the moved items, in the order that the user selected them. It also contains a destination value, which indicates where to insert the item.
This example shows a list of reminders. Items can be moved within the list’s underlying collections.
struct ContentView: View {
@State private var model = ReminderModel()
var body: some View {
List {
ForEach(model.sections) { section in
Section(section.name) {
ForEach(section.reminders) { reminder in
ReminderView(reminder)
}
.reorderable(collectionID: section.id)
}
}
}
.reorderContainer(
for: Reminder.self, in: ReminderModel.Section.ID.self
) { difference in
model.apply(difference: difference)
}
}
}item: The type of reorderable items in the container.
collectionID: The type used to identify collections of reorderable items in the container.
isEnabled: Whether the container allows reordering.
move: A closure that provides the change at the end of a session.
See Also
Reordering items
Making a card game with drag, drop, and reordering in SwiftUIreorderable()reorderable(collectionID:)ReorderableSingleCollectionIdentifierreorderContainer(for:isEnabled:move:)reorderContainer(for:itemID:isEnabled:move:)reorderContainer(for:itemID:in:isEnabled:move:)reorderDestination(for:in:)reorderDestination(for:itemID:in:)ReorderDifference