reorderContainer
Defines a container of reorderable views, with a type you specify to identify sections.
Overloads
2 ways to call compose().reorderContainer(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().reorderContainer({
isEnabled?: boolean,
move: (difference: SwiftUI.ReorderDifference) => void,
})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 : Sendable1 more Swift overload share this form
nonisolated func reorderContainer<Item>(
for item: Item.Type,
isEnabled: Bool = true,
move: @escaping (ReorderDifference<Item.ID, ReorderableSingleCollectionIdentifier>) -> ()
) -> some View
where Item : Identifiable, Item.ID : SendableParameters
options.isEnabledbooleanWhether the container allows reordering.
options.move*(difference: SwiftUI.ReorderDifference) => voidA 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.
- alignmentGuideSets the view's vertical alignment.
- aspectRatioConstrains this view's dimensions to the specified aspect ratio.
- containerCornerOffsetAdjusts the view's layout to avoid the container view's corner insets for the specified edges.
- containerRelativeFramePositions this view within an invisible frame with a size relative to the nearest container.
- controlSizeSets the size for controls within this view.
- defaultScrollAnchorAssociates an anchor to control which part of the scroll view's content should be rendered by default.
- dynamicTypeSizeSets the Dynamic Type size within the view to the given value.
- fixedSizeFixes this view at its ideal size.
- flipsForRightToLeftLayoutDirectionSets whether this view mirrors its contents horizontally when the layout direction is right-to-left.
- framePositions this view within an invisible frame.
- geometryGroupIsolates the geometry (e.g. position and size) of the view from its parent view.
- gridCellAnchorSpecifies a custom alignment anchor for a view that acts as a grid cell.