ios - Swift UI List auto scrolls up when animating data source changes - Stack Overflow

admin2025-04-18  3

I have noticed an annoying behaviour Swift UI Lists (iOS 18 at least) where, if anything changes in the list's data source, for some reason everything scrolls up.

I've been careful to only change the needed item in the observed data source, but even by doing that the result don't change. Example:

A possible suspect it's because by marking an element as read, then the element moves to a different section. But even then, I don't get why this happens. Do you have any suggestion? Some code:

Section {
    MyList()
        .swipeActions(edge: .leading, allowsFullSwipe: true) {
            ReadButton {
                // NOTE: This is needed to let the swipe action finish its animation
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    withAnimation { markNotificationAsRead(id) }
                    // Operates on the data source by marking that notification as read
                }
            }
        }
}


...
// List code:

List {
    ForEach(state.sections, id: \.id) { section in
        inboxSection(section: section)
    }
}
.animation(.default, value: state.sections) // This creates the problem
转载请注明原文地址:http://anycun.com/QandA/1744905829a89287.html