Search code examples
iosswiftswiftui

List lacks delete animation when custom cell is not surrounded by HStack


I have a List in SwiftUI which includes custom cells for each item of an array. The cells are basically just DisclosureGroups with a button to remove the specific item from the list. To summarize: I have a list, where each cell has a button to remove itself.

I unexpectedly noticed, that the removing animation was not smooth at all.

I added the following modifier to the list .animation(.default, value: model.tasks) and surrounded the code for the deletion in the array with withAnimation {}

model.tasks is the array used to populate the list. It stores an enum which conforms to Identifiable, Equatable and CaseIterable.

Example No.1

List {
    ForEach(model.tasks, id: \.self) { task in
        NewCell(task: task)
            .environmentObject(model)
     }
}

try no1

Notice how the cell removes smoothly but the content does not.

Example No.2 This time I surrounded the cell with an HStack

List {
    ForEach(model.tasks, id: \.self) { task in
        HStack {
            NewCell(task: task)
                .environmentObject(model)
        }
     }
}

try no.2

This looks exactly how I like it to be.

What is the reason for this strange behavior?

EDIT: I actually need to get rid of HStack or VStack, because it will mess up the DisclosureGroup. So I need a good solution for that animation issue.


Solution

  • Back then I did raise a Feedback at Apple. And apparently it was fixed in one of the most recent versions of iOS. No code change needed, especially no extra HStack.