Saturday, November 7, 2026
HomeiOS Developmentios - Why Animation does not work inside a baby view handed...

ios – Why Animation does not work inside a baby view handed by Binding?

[ad_1]

I’ve this bizarre downside the place after I cross a binding to a baby view, adjustments inside this baby view will not be animated apart from .onDelete modifier.

Here’s a simplified code that produces this downside:

Observable(ViewModel) and mannequin struct:

extension ContentView {
    class ViewModel: ObservableObject {
        @Printed var customers = [User]()

        func createUser() {
            customers.append(Person(occasions: []))
        }
    }

    struct Person: Equatable {
        let identify = UUID().uuidString
        var occasions: [Date]
    }
}

ContentView: (Dad or mum view)

struct ContentView: View {
    @StateObject var vm = ViewModel()

    var physique: some View {
        NavigationView {
            Listing {
                ForEach(vm.customers.indices, id: .self) { index in
                    NavigationLink {
                        UserView(person: $vm.customers[index])
                    } label: {
                        Textual content(vm.customers[index].identify)
                    }
                }
                .onDelete { offsets in
                    vm.customers.take away(atOffsets: offsets)
                }
                Button("Add") {
                    withAnimation {
                        vm.createUser()
                    }
                }
            }
            .navigationTitle("Dad or mum View")
        }
    }
}

UserView (Little one View):

struct UserView: View {
    @Binding var person: ContentView.Person

    var physique: some View {
        Listing {
            ForEach(person.occasions.indices, id: .self) { index in
                Textual content(person.occasions[index].description)
            }
            .onDelete { offsets in
                person.occasions.take away(atOffsets: offsets)
            }
            Button {
                withAnimation {
                    person.occasions.append(Date())
                }
            } label: {
               Textual content("Add Time")
            }

        }
        .navigationTitle("Little one View")
    }
}

Discover in beneath picture that the mum or dad view having nearly precisely the identical code apart from binding is working and animating as meant, and see the kid view .onDelete modifier can be animating because it meant however when including to the checklist of kid view, animation will not be working.

Thanks!
enter image description here

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments