SwiftUI List views size bug (iOS 16)

Hello everyone. I just discovered one issue with List in iOS16. The sample Playground code is provided below. Steps to reproduce:

  1. Run the code.
  2. Scroll down the list to the bottom so first rows become hidden.
  3. Scroll up to first row.
  4. Tap on first row.

Result: row is marked as selected but its size stays unchanged. Once you scroll the list up and down this row will resize itself. Expected result: row resize immediately after tap on it.

Note that if you don't perform steps 2 and 3 (maybe cells are not being reused) it works as expected. Also on iOS 14, 15 it works as expected


import PlaygroundSupport



struct Item: Identifiable, Equatable {

    let id: String

    let name: String

}



struct App: View {

    

    @State var items: [Item] =   [Item(id: "0", name: "Apples"),

                                  Item(id: "1", name: "Oranges"),

                                  Item(id: "2", name: "Bananas"),

                                  Item(id: "3", name: "Pears"),

                                  Item(id: "4", name: "Mangos"),

                                  Item(id: "5", name: "Grapefruit"),

                                  Item(id: "6", name: "Peaches"),

                                  Item(id: "7", name: "Qiwies"),

                                  Item(id: "8", name: "Strawberries"),

                                  Item(id: "9", name: "Lemons"),

                                  Item(id: "10", name: "Limes"),

                                  Item(id: "11", name: "Pineapples")]

    @State var selections: [Item] = []

   

    var body: some View {

        List(items) { item in

            MultipleSelectionRow(title: item.name, isSelected: selections.contains(item)) {

                if selections.contains(item) {

                    selections.removeAll(where: { $0 == item })

                }

                else {

                    selections.append(item)

                }

            }

        }

    }

}



struct MultipleSelectionRow: View {

    var title: String

    var isSelected: Bool

    var action: () -> Void

    

    var body: some View {

        HStack {

            Text(title)

            if isSelected {

                Spacer()

                Image(systemName: "checkmark")

            }

        }

        .frame(height: isSelected ? 120 : 60)

        .onTapGesture {

            action()

        }

    }

}

PlaygroundPage.current.setLiveView(App().previewDevice("iPhone 13"))

I also found this bug in iOS16. The list animation seems to prevent the frame from changing. For now, I'm temporarily handling it by adding .id(UUID()) to the cell. If there is a better solution please let me know.

SwiftUI List views size bug (iOS 16)
 
 
Q