skip to content

On iOS 27, how do SwiftUI's reorderable() and reorderContainer(for:isEnabled:move:) differ from onMove when a mail client lets people drag its folders into a new order?

level: seniorimportance: should knowfreq 32%

answer

  1. offsets versus identifiers
  2. a modifier on content and on container
  3. before an item, or at the end
  4. not only inside List

basics

~20 s

onMove gives a List's ForEach index-based moves; iOS 27's reorderable() marks a ForEach movable inside any list, stack, grid or layout carrying reorderContainer(for:isEnabled:move:), whose closure gets a ReorderDifference of item IDs and a before-or-end destination.

solid answer

~40 s

`onMove(perform:)` (iOS 13) sits on a `ForEach` in a `List`; in edit mode the list shows move controls and calls you with an `IndexSet` of source offsets and an `Int` destination, which `move(fromOffsets:toOffset:)` applies. iOS 27 adds a two-part API: `reorderable()` on the `ForEach` and `reorderContainer(for:isEnabled:move:)` on the enclosing list, stack, grid or custom layout. Its closure receives a `ReorderDifference`: `sources`, the moved items' IDs in the order the person picked them, and a `destination` that is `.before(id)` or `.end`. Because it speaks in identifiers, Apple says you can apply it even while a sync changes the array; `isEnabled` turns reordering off during a save. Apps deploying to iOS 17 or 18 keep the `onMove` path behind `if #available(iOS 27, *)`.

code

swift · 29 lines
swift
import SwiftUI

struct Folder: Identifiable { let id = UUID(); var name: String }

@available(iOS 27.0, *)
struct FolderList: View {
    @State private var folders: [Folder] = []
    @State private var isSyncing = false

    var body: some View {
        List {
            ForEach(folders) { folder in
                Label(folder.name, systemImage: "folder")
            }
            .reorderable()
        }
        .reorderContainer(for: Folder.self, isEnabled: !isSyncing) { difference in
            let moving = difference.sources.compactMap { id in folders.first { $0.id == id } }
            folders.removeAll { difference.sources.contains($0.id) }
            switch difference.destination.position {
            case .before(let anchor):
                let index = folders.firstIndex { $0.id == anchor } ?? folders.endIndex
                folders.insert(contentsOf: moving, at: index)
            case .end:
                folders.append(contentsOf: moving)
            }
        }
    }
}

go deeper

for a junior

Recall that onMove on a ForEach lets a List reorder rows in edit mode, and that iOS 27 adds reorderable with a reorder container.

for a middle

Explain onMove's offsets, the two-part iOS 27 API, and the shape of ReorderDifference: source IDs and a before-or-end destination.

for a senior

Apply differences by identifier, disable reordering during sync, handle several sources, and keep an availability-gated onMove fallback.

for a principal

Decide when the app adopts identifier-based reordering everywhere, weighing iOS 27 adoption against a dual code path for older systems.

## The classic API: onMove(perform:) Since iOS 13, a `ForEach` inside a `List` can declare **`onMove(perform:)`**. The behaviour: - When **edit mode** is active, Apple's documentation says the list shows controls to move rows. - When someone drops a row, SwiftUI calls the closure with an **`IndexSet`** of source offsets and an **`Int`** destination offset. - SwiftUI adds `move(fromOffsets:toOffset:)` to mutable collections, so the usual body is `folders.move(fromOffsets: source, toOffset: destination)`. The model is **positional**. The offsets describe the array as it looked when the drag started. If the data changes during the drag — a sync inserts a folder at the top — those offsets can point at the wrong elements. Apple documents the move controls as a `List` behaviour in edit mode; its reordering article presents the iOS 27 modifiers as the way to add drag-to-reorder to stacks, grids and custom layouts. ## The iOS 27 API: reorderable() plus reorderContainer The iOS 27 SDK splits reordering into two modifiers: 1. **`reorderable()`** on the `ForEach` (any `DynamicViewContent`) marks which views can be lifted. Apple's documentation says it enables reordering "inside the scope of a reorderable container modifier", so on its own it does nothing. 2. **`reorderContainer(for:isEnabled:move:)`** on the enclosing **list, stack, grid or custom layout** defines that scope. `for:` takes the item type, which must be `Identifiable` with a `Sendable` ID; an overload with `itemID:` takes a key path when the type is not `Identifiable`. When a person lifts a view, the system leaves a placeholder and moves it as they drag. At the end of the session it calls `move` with a **`ReorderDifference`**: - **`sources`** — an array of the moved items' IDs, in the order the person selected them. - **`destination`** — a `Destination` with a `position` of **`.before(id)`** or **`.end`**, plus a `collectionID`. ```swift .reorderContainer(for: Folder.self, isEnabled: !isSyncing) { difference in let moving = difference.sources.compactMap { id in folders.first { $0.id == id } } folders.removeAll { difference.sources.contains($0.id) } switch difference.destination.position { case .before(let anchor): folders.insert(contentsOf: moving, at: folders.firstIndex { $0.id == anchor } ?? folders.endIndex) case .end: folders.append(contentsOf: moving) } } ``` Because the difference names **items** and an **anchor item** rather than positions, Apple notes you can apply it "even if other changes to the array, such as a cloud sync, occur simultaneously". ## Side by side | | `onMove(perform:)` | `reorderable()` + `reorderContainer` | |---|---|---| | Available | iOS 13 | iOS 27; unavailable on tvOS | | Containers | `List` | list, stack, grid, custom layout | | Change format | `IndexSet` + destination offset | item IDs + `.before(id)` or `.end` | | Several items | offsets in one set | `sources` in the order picked | | Turning it off | per row with `moveDisabled(_:)` | whole container with `isEnabled:` | | Several collections | one `onMove` per `ForEach` | `reorderable(collectionID:)` with `reorderContainer(for:in:isEnabled:move:)` | For a mail client whose folders are grouped by account, the multi-collection form lets a person move a folder within an account or to another one: each account's `ForEach` gets `reorderable(collectionID: account.id)`, and the destination's `collectionID` says where it landed. ## Shipping it while older iOS versions remain - Most apps still deploy to iOS 17 or 18, so wrap the new path in `if #available(iOS 27, *)` and keep the `onMove` version for older systems. - Disable reordering with `isEnabled:` while a save or sync is in flight, as Apple's own example does. - Drags that leave the container, to another window or app, are handled by separate drag-and-drop modifiers; the lesson on drag, drop and sharing covers them. ## Choosing between them in one codebase For a screen that only ever lives in a `List` and only targets iOS 27, the new API is the better default: identifiers instead of offsets, multi-item moves and a single switch to pause reordering. For a screen that must also run on iOS 17 or 18, the realistic shape is two code paths, with the `onMove` branch converting offsets to identifiers before touching the model so both branches end in the same model call, such as `moveFolders(ids:before:)`. That keeps the model's reorder logic tested once, whichever gesture produced the change. ## Mistakes to catch in review - Adding `reorderable()` without a `reorderContainer` around it. - Converting the difference back to offsets early and reintroducing the stale-position problem. - Ignoring that `sources` can hold several IDs and inserting only the first. - Calling the iOS 27 modifiers in a target that still deploys to iOS 17 without an availability check.

  • How would you let people move folders between two accounts on iOS 27?
    Give each account's `ForEach` `reorderable(collectionID: account.id)` and put `reorderContainer(for: Folder.self, in: Account.ID.self) { … }` on the enclosing container. The `ReorderDifference` destination then carries the target account's `collectionID` with `.before(id)` or `.end`, so you remove the sources from their accounts and insert them into that one.
  • Why prefer the ReorderDifference path over onMove when folders sync from a server?
    `onMove` reports positions in the array as it was when the drag began, so an insertion during the drag shifts what those offsets mean. A `ReorderDifference` names the moved items and the item to insert before, which Apple says you can apply even while other changes, such as a cloud sync, arrive. You can also turn reordering off with `isEnabled:` while saving.

Telling a mover to put the third box before the fifth goes wrong if someone adds a box in the meantime; telling them to put the Invoices box before the Receipts box still works. onMove speaks in box numbers, while a ReorderDifference speaks in box labels.

  • reorderable() alone makes a ForEach draggable without any container modifier
  • reorderContainer's move closure receives an IndexSet and a destination offset
  • The iOS 27 reordering API works only inside a List
  • ReorderDifference holds a single moved item
  • reorderable() can be used unguarded in an app that deploys to iOS 17

Assumes the iOS 27 SDK: reorderable(), reorderContainer and ReorderDifference are iOS 27 APIs, while onMove(perform:) has been available since iOS 13.