How to start SwiftUI List onDelete action with VoiceOver?

62 Views Asked by At

I have SwiftUI List in edit mode (editMode == .active) and with onDelete modifier on some of list rows. I would like to make onDelete action available with Accessibility (VoiceOver). How do I do that?

Without any additional code, delete accessory (red circle with minus sign) on list element is not visible for Accessibility (swiping through accessibility elements just skips over this accessory).

2

There are 2 best solutions below

0
Taeeun Kim On

I assume that onDelete is specifically for swipe actions.
Therefore, for accessibilityAction, I needed to add an additional function for removal.

Example:

struct ContentView: View {
    @State private var items = ["Item 1", "Item 2", "Item 3"]

    var body: some View {
        List {
            ForEach(items.indices, id: \.self) { index in
                Text(items[index])
                    .accessibility(label: Text("Item \(items[index])"))
                    .accessibility(hint: Text("Double-tap to delete"))
                    .accessibilityAction {
                        deleteItemFromVoiceOver(at: index)
                    }
            }
            .onDelete(perform: deleteItemFromSwipe)
        }
    }

    func deleteItemFromSwipe(at offsets: IndexSet) {
        // for swipe action
        items.remove(atOffsets: offsets)
    }

    func deleteItemFromVoiceOver(at index: Int) {
        // for VoiceOver
        items.remove(at: index)
    }
}
0
Suprafen On

You need to apply onDelete modifier, and SwiftUI will do the rest by providing Voice Over user with the next hint: “<Row element’s label, value, etc.>. Swipe up or down to select a custom action. Then double tap to activate.

Even though you add EditButton() to your toolbar VoiceOver won’t allow you to interact with delete buttons(the red one with minus sign).


I assume it was made intentionally, to avoid making additional effort for VO user.