reorderContainer

Defines a container of reorderable views, with a type you specify to identify sections.

View ModifieriOS 27.0+macOS 27.0+watchOS 27.0+visionOS 27.0+

Overloads

2 ways to call compose().reorderContainer(). Each is one SwiftUI overload, picked by the arguments you pass.

React
compose().reorderContainer({
  isEnabled?: boolean,
  move: (difference: SwiftUI.ReorderDifference) => void,
})
Swift
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
1 more Swift overload share this form
Swift
nonisolated func reorderContainer<Item>(
    for item: Item.Type,
    isEnabled: Bool = true,
    move: @escaping (ReorderDifference<Item.ID, ReorderableSingleCollectionIdentifier>) -> ()
) -> some View
where Item : Identifiable, Item.ID : Sendable

Parameters

  • options.isEnabledboolean

    Whether the container allows reordering.

  • options.move*(difference: SwiftUI.ReorderDifference) => void

    A closure that provides the change at the end of a session.

Discussion

Declare this modifier on your list, stack, grid, or custom layout to define a reorderable container. Then, apply DynamicViewContent.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 View.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

More in Layout.