reorderContainer(for:in:isEnabled:move:)
Defines a container of reorderable views, with a type you specify to identify sections.
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
Parameters
- 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.
Mentioned in
Discussion
Declare this modifier on your list, stack, grid, or custom layout to define a reorderable container. Then, apply reorderable(collectionID:) to the content of your container to make those views reorderable.
Use this overload if your container contains multiple collections. If your container only has a single collection, use the convenience reorderContainer(for:isEnabled:move:) modifier.
A person can lift a reorderable view within the container using a drag gesture. As they lift the item, the system puts a placeholder view in its place to indicate where the view can drop. As they move the item through the container, the position of the placeholder updates to reflect which view the person drags over. When they drop the view, the system calls the move closure and provides the change.
The system provides the change as a difference to the closure. The difference contains the identifiers of items to move, in the order that the person selected them. It also contains a destination value, which indicates where to insert the item or items.
The following example shows a list of reminder views that a person can move to reorder inside and between each Section in the List:
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)
}
}
}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