From 00342c1b3a9061a6bfaf7444a133c465598b623c Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Tue, 4 Mar 2025 12:53:40 -0500 Subject: [PATCH 1/2] Add double scrollTo to fix scroll restoration inconsistency --- .../Layout Containers/Collection Containers/List.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift b/Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift index 6d7a02ae8..080490b12 100644 --- a/Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift +++ b/Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift @@ -208,6 +208,7 @@ struct List: View { } } } + .opacity(didAttemptRestoration ? 1 : 0) // prevent flickering when restoring .task { defer { didAttemptRestoration = true } @@ -216,6 +217,12 @@ struct List: View { else { return } scrollProxy.scrollTo(restoreID, anchor: .top) + + // wait for the next runloop and try to scroll again + // it seems that the scroll won't work if the elements are not laid-out yet + try! await Task.sleep(for: .nanoseconds(0)) + + scrollProxy.scrollTo(restoreID, anchor: .top) } } } From 2b0b1b7bb3a2b2f196921e82ae7ec87f8ec98362 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Tue, 4 Mar 2025 12:59:56 -0500 Subject: [PATCH 2/2] Show by default when scroll restoration is disabled --- .../Views/Layout Containers/Collection Containers/List.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift b/Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift index 080490b12..e730367ea 100644 --- a/Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift +++ b/Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift @@ -208,7 +208,7 @@ struct List: View { } } } - .opacity(didAttemptRestoration ? 1 : 0) // prevent flickering when restoring + .opacity((id == nil || didAttemptRestoration) ? 1 : 0) // prevent flickering when restoring .task { defer { didAttemptRestoration = true }