Correct animation for changing cells in List/ForEach loop on search in SwiftUI?

115 views Asked by At

The problem I am having with the below code is that when the user searches for a specific contact and I need to change the list from contacts to searchResults (which contain the same type of CNContact element with unique .identifier, the animation slides the entire list to the right and bring another one from the left. Surprisingly, this doesn't reproduce in iOS 14, so maybe this is a bug with iOS 13.4?

var body: some View {
    VStack {
        SearchBar(text: $searchViewModel.searchText)
        let displayContacts = searchViewModel.searchText.count == 0 ? contacts : searchResults
        List {
            ForEach(displayContacts, id: \.identifier) { contact in
                cellWithContact(contact)
            }
        }
    }
}

@ViewBuilder
private func cellWithContact(_ contact: CNContact) -> some View {
    let displayName: String? = CNContactFormatter.string(from: contact, style: .fullName)
    HStack() {
        Text(displayName ?? "No Name")
        Spacer()
        Button {
            // invite
        } label: {
            Text("Invite")
        }
    }
}

enter image description here

0

There are 0 answers