SwiftUI - list with multi-select (not a custom one)

Viewed 1654

How to make a list with multi-select in SwiftUI? I know I can make a custom one like this: https://stackoverflow.com/a/57023746/12315994

But is there a default one already in SwiftUI?

Here is an example of multi-select controls in Apple's Mail app:

Apple Mail app

The same controls are also used in Apple Photos when you select multiple photos. These controls are also in Apple's official iOS Sketch Library which you can download from here: https://developer.apple.com/design/resources/

There are very similar controls in Apple's Reminders App:

Apple Reminders app

1 Answers

Yes, SwiftUI's list has this capability built in. You need to provide a Set for the selection parameter of the list for multiple selections. I'm also setting the edit mode to .active by default, which is optional.

struct ContentView: View {
    @State private var selection = Set<String>()
    @State private var isEditMode: EditMode = .active
    
    let items = [
        "Item 1",
        "Item 2",
        "Item 3",
        "Item 4"
    ]

    var body: some View {
        NavigationView {
            List(items, id: \.self, selection: $selection) { name in
                Text(name)
            }
            .toolbar {
                EditButton()
            }
            .environment(\.editMode, self.$isEditMode)
        }
    }
}

Related