Skip to main content

How to enable editing on a list using EditButton

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to enable editing on a list using EditButton 관련

SwiftUI by Example

Back to Home

How to enable editing on a list using EditButton | SwiftUI by Example

How to enable editing on a list using EditButton

Updated for Xcode 15

If you have configured a SwiftUI list view to support deletion or editing of its items, you can allow the user to toggle editing mode for your list view by adding an EditButton somewhere.

For example, this ContentView struct defines an array of users, attaches an onDelete() method, then adds an edit button to the navigation bar:

struct ContentView: View {
    @State private var users = ["Paul", "Taylor", "Adele"]

    var body: some View {
        NavigationStack {
            List {
                ForEach(users, id: \.self) { user in
                    Text(user)
                }
                .onDelete(perform: delete)
            }
            .toolbar {
                EditButton()
            }
        }
    }

    func delete(at offsets: IndexSet) {
        users.remove(atOffsets: offsets)
    }
}

Download this as an Xcode projectopen in new window

When that is run, you'll find you can tap the edit button to enable or disable editing mode for the items in the list.

Similar solutions…
Adding swipe to delete and EditButton | SwiftUI by Example

Adding swipe to delete and EditButton
How to enable pull to refresh | SwiftUI by Example

How to enable pull to refresh
How to enable vertical page scrolling | SwiftUI by Example

How to enable vertical page scrolling
How to allow row selection in a list | SwiftUI by Example

How to allow row selection in a list
How to let users move rows in a list | SwiftUI by Example

How to let users move rows in a list

이찬희 (MarkiiimarK)
Never Stop Learning.