Fix scrolling view on resource change

This commit is contained in:
Arkadiusz Fal 2021-09-30 01:29:18 +02:00
parent e270b7b213
commit 3a37ea656a
3 changed files with 13 additions and 18 deletions

View File

@ -33,7 +33,7 @@
<BuildableReference <BuildableReference
BuildableIdentifier = "primary" BuildableIdentifier = "primary"
BlueprintIdentifier = "37D4B0D32671614900C925CA" BlueprintIdentifier = "37D4B0D32671614900C925CA"
BuildableName = "Tests iOS.xctest" BuildableName = "Tests (iOS).xctest"
BlueprintName = "Tests (iOS)" BlueprintName = "Tests (iOS)"
ReferencedContainer = "container:Pearvidious.xcodeproj"> ReferencedContainer = "container:Pearvidious.xcodeproj">
</BuildableReference> </BuildableReference>

View File

@ -9,6 +9,8 @@ struct VideosCellsHorizontal: View {
var videos = [Video]() var videos = [Video]()
var body: some View { var body: some View {
EmptyView().id("cellsTop")
ScrollViewReader { scrollView in ScrollViewReader { scrollView in
ScrollView(.horizontal, showsIndicators: false) { ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 20) { LazyHStack(spacing: 20) {
@ -32,19 +34,12 @@ struct VideosCellsHorizontal: View {
.padding(.vertical, 20) .padding(.vertical, 20)
#endif #endif
} }
.onAppear {
if let video = videos.first {
scrollView.scrollTo(video.id, anchor: .leading)
}
}
.onChange(of: videos) { [videos] newVideos in .onChange(of: videos) { [videos] newVideos in
#if !os(tvOS) guard !videos.isEmpty else {
guard !videos.isEmpty, let video = newVideos.first else { return
return }
}
scrollView.scrollTo(video.id, anchor: .leading) scrollView.scrollTo("cellsTop", anchor: .leading)
#endif
} }
} }
#if os(tvOS) #if os(tvOS)

View File

@ -9,6 +9,8 @@ struct VideosCellsVertical: View {
var videos = [Video]() var videos = [Video]()
var body: some View { var body: some View {
EmptyView().id("cellsTop")
ScrollViewReader { scrollView in ScrollViewReader { scrollView in
ScrollView(.vertical, showsIndicators: scrollViewShowsIndicators) { ScrollView(.vertical, showsIndicators: scrollViewShowsIndicators) {
LazyVGrid(columns: items, alignment: .center) { LazyVGrid(columns: items, alignment: .center) {
@ -22,13 +24,11 @@ struct VideosCellsVertical: View {
.padding() .padding()
} }
.onChange(of: videos) { [videos] newVideos in .onChange(of: videos) { [videos] newVideos in
#if !os(tvOS) guard !videos.isEmpty else {
guard !videos.isEmpty, let video = newVideos.first else { return
return }
}
scrollView.scrollTo(video.id, anchor: .top) scrollView.scrollTo("cellsTop", anchor: .top)
#endif
} }
#if os(tvOS) #if os(tvOS)
.padding(.horizontal, 10) .padding(.horizontal, 10)