iOS Development

ios – No animation in subview utilizing NavigationStack in SwiftUI

Spread the love


I’ve an issue with animations not working when my view is seen from its mother or father utilizing NavigationStack. I’ve 2 easy views, mother or father:

struct ListEntitiesView: View {
    @Surroundings(.managedObjectContext)
    personal var viewContext
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: ListEntity.name, ascending: true)],
        animation: .default
    )
    personal var lists: FetchedResults<ListEntity>
    @State personal var path = NavigationPath()

    var physique: some View {
        NavigationStack(path: $path.animation()) {
            Checklist {
                ForEach(lists) { listing in
                    NavigationLink(worth: listing) {
                        ListEntityTile(listing)
                    }
                }
            }
            .navigationTitle("ListEntities")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                Button {
                    let listing = ListEntity(context: viewContext)
                    listing.title = "Checklist (lists.depend + 1)"
                    viewContext.forceSave()
                } label: {
                    Label("Add Checklist", systemImage: "plus")
                }
            }
            .navigationDestination(for: ListEntity.self) { listing in
                ListEntityView(listing)
            }
        }
    }
}

and little one:

struct ListEntityView: View {
    @Surroundings(.managedObjectContext)
    personal var viewContext
    @ObservedObject
    personal var listing: ListEntity
    @FetchRequest
    personal var gadgets: FetchedResults<ListItemEntity>

    init(_ listing: ListEntity) {
        _list = ObservedObject(wrappedValue: listing)
        _items = FetchRequest(
            fetchRequest: ListItemEntity.fetchRequest(
                of: listing,
                sortDescriptors: [NSSortDescriptor(keyPath: ListItemEntity.name?.name, ascending: true)]
            ),
            animation: .default
        )
    }

    var physique: some View {
        Checklist {
            ForEach(gadgets) { merchandise in
                ListItemTile(merchandise)
            }
        }
        .navigationTitle(listing.wrappedName)
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            NavigationLink {
                AddListItemEntity(listing)
            } label: {
                Label("Add Merchandise", systemImage: "plus")
            }
        }
    }
}

struct ListItemTile: View {
    @Surroundings(.managedObjectContext)
    personal var viewContext
    @ObservedObject
    personal var merchandise: ListItemEntity

    init(_ merchandise: ListItemEntity) {
        _item = ObservedObject(wrappedValue: merchandise)
    }

    var physique: some View {
        Button {
            merchandise.flag.toggle()
            viewContext.forceSave()
        } label: {
            Label(merchandise.wrappedName, systemImage: merchandise.flag ? "checkmark.circle" : "circle")
        }
    }
}

Within the little one Preview, if I create it inside NavigationStack every little thing animates as anticipated:

#Preview {
    let listing = ListEntity(context: DataProvider.shared.viewContext)
    listing.title = "Checklist"
    let title = NameEntity(context: DataProvider.shared.viewContext)
    title.title = "Title"
    let merchandise = ListItemEntity(context: DataProvider.shared.viewContext)
    merchandise.flag = false
    merchandise.listing = listing
    merchandise.title = title
    DataProvider.shared.viewContext.forceSave()
    return NavigationStack {
        ListEntityView(listing)
            .surroundings(.managedObjectContext, DataProvider.shared.viewContext)
    }
}

However as soon as view is inside NavigationLink (or created in mother or father’s navigationDestination) nothing animates:

#Preview {
    let listing = ListEntity(context: DataProvider.shared.viewContext)
    listing.title = "Checklist"
    let title = NameEntity(context: DataProvider.shared.viewContext)
    title.title = "Title"
    let merchandise = ListItemEntity(context: DataProvider.shared.viewContext)
    merchandise.flag = false
    merchandise.listing = listing
    merchandise.title = title
    DataProvider.shared.viewContext.forceSave()
    return NavigationStack {
        NavigationLink {
            ListEntityView(listing)
        } label: {
            Textual content("Checklist particulars")
        }
    }
    .surroundings(.managedObjectContext, DataProvider.shared.viewContext)
}

I’ve discovered a thread on Apple’s developer discussion board, however resolution from there does not assist in my case. Some other concepts? Or perhaps my code have a bug in it I do not see?

Leave a Reply

Your email address will not be published. Required fields are marked *