From 3a37ea656a1f4d9f1a4dc74d19e9bf25a5d53c31 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Thu, 30 Sep 2021 01:29:18 +0200 Subject: [PATCH] Fix scrolling view on resource change --- .../xcschemes/Pearvidious (iOS).xcscheme | 2 +- Shared/Videos/VideosCellsHorizontal.swift | 17 ++++++----------- Shared/Videos/VideosCellsVertical.swift | 12 ++++++------ 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/Pearvidious.xcodeproj/xcshareddata/xcschemes/Pearvidious (iOS).xcscheme b/Pearvidious.xcodeproj/xcshareddata/xcschemes/Pearvidious (iOS).xcscheme index 88e8d25c..f1fb5cdb 100644 --- a/Pearvidious.xcodeproj/xcshareddata/xcschemes/Pearvidious (iOS).xcscheme +++ b/Pearvidious.xcodeproj/xcshareddata/xcschemes/Pearvidious (iOS).xcscheme @@ -33,7 +33,7 @@ diff --git a/Shared/Videos/VideosCellsHorizontal.swift b/Shared/Videos/VideosCellsHorizontal.swift index 03f6ea75..4fcd9eef 100644 --- a/Shared/Videos/VideosCellsHorizontal.swift +++ b/Shared/Videos/VideosCellsHorizontal.swift @@ -9,6 +9,8 @@ struct VideosCellsHorizontal: View { var videos = [Video]() var body: some View { + EmptyView().id("cellsTop") + ScrollViewReader { scrollView in ScrollView(.horizontal, showsIndicators: false) { LazyHStack(spacing: 20) { @@ -32,19 +34,12 @@ struct VideosCellsHorizontal: View { .padding(.vertical, 20) #endif } - .onAppear { - if let video = videos.first { - scrollView.scrollTo(video.id, anchor: .leading) - } - } .onChange(of: videos) { [videos] newVideos in - #if !os(tvOS) - guard !videos.isEmpty, let video = newVideos.first else { - return - } + guard !videos.isEmpty else { + return + } - scrollView.scrollTo(video.id, anchor: .leading) - #endif + scrollView.scrollTo("cellsTop", anchor: .leading) } } #if os(tvOS) diff --git a/Shared/Videos/VideosCellsVertical.swift b/Shared/Videos/VideosCellsVertical.swift index 8d0dd9b5..8c34f1b0 100644 --- a/Shared/Videos/VideosCellsVertical.swift +++ b/Shared/Videos/VideosCellsVertical.swift @@ -9,6 +9,8 @@ struct VideosCellsVertical: View { var videos = [Video]() var body: some View { + EmptyView().id("cellsTop") + ScrollViewReader { scrollView in ScrollView(.vertical, showsIndicators: scrollViewShowsIndicators) { LazyVGrid(columns: items, alignment: .center) { @@ -22,13 +24,11 @@ struct VideosCellsVertical: View { .padding() } .onChange(of: videos) { [videos] newVideos in - #if !os(tvOS) - guard !videos.isEmpty, let video = newVideos.first else { - return - } + guard !videos.isEmpty else { + return + } - scrollView.scrollTo(video.id, anchor: .top) - #endif + scrollView.scrollTo("cellsTop", anchor: .top) } #if os(tvOS) .padding(.horizontal, 10)