Skip to content
DeveloperMemos

Using SwiftUI's onMove to Re-order a List

SwiftUI, Lists, User Interface2 min read

When developing an iOS app, creating an intuitive and smooth user interface is crucial. SwiftUI offers a variety of tools to accomplish this, including the ability to allow users to reorder lists effortlessly. In this article, we'll explore how to leverage SwiftUI's onMove functionality to implement reordering of lists within your iOS applications. We'll delve into its practical applications and provide example code snippets to help you integrate this feature seamlessly.

Understanding SwiftUI's onMove

Before diving into the implementation details, let's first understand what SwiftUI's onMove function is and how it can enhance the user experience. The onMove modifier is used to enable the reordering of elements within a list. When applied to a SwiftUI view, such as a List, it provides users with the ability to rearrange items by dragging them to new positions in the list.

Implementing onMove in SwiftUI

To implement reordering using SwiftUI's onMove, start by creating a basic SwiftUI view containing a list of items that you want to make reorderable. Then, apply the onMove modifier to the list along with any other necessary modifiers or data sources. Here's an example demonstrating how to use onMove within a SwiftUI view:

1struct ReorderableListView: View {
2 @State private var items = ["Item 1", "Item 2", "Item 3", "Item 4"]
3
4 var body: some View {
5 List {
6 ForEach(items, id: \.self) { item in
7 Text(item)
8 }
9 .onMove { indices, newOffset in
10 self.items.move(fromOffsets: indices, toOffset: newOffset)
11 }
12 }
13 }
14}

In this example, the onMove modifier is added to the List and a closure is provided to handle the reordering action. When the user initiates a move operation, the closure is called, updating the state variable items to reflect the new order based on the provided indices and new offset.

Customizing Reordering Behavior

SwiftUI's onMove also allows for customizing the behavior during reordering. This includes controlling where items can be moved, such as restricting certain items from being repositioned. Additionally, you can add animations to provide visual feedback when items are moved within the list.

Practical Applications

The capability to reorder lists becomes particularly powerful when applied to various types of content within iOS apps. For instance, consider a to-do list application where users can prioritize tasks by rearranging them, or an image gallery app that enables users to organize their photo albums with a simple drag-and-drop gesture. Utilizing onMove empowers users to intuitively manage and customize the content they interact with, ultimately enhancing the overall user experience.


Incorporating the onMove functionality in SwiftUI is a valuable technique for improving the interactivity and usability of your iOS applications. By allowing users to reorder lists through a simple drag-and-drop mechanism, you can provide a more dynamic and engaging experience. Whether it's for managing tasks, organizing content, or streamlining user interactions, integrating reordering functionality using onMove enhances the usability and appeal of your SwiftUI-based apps.