0

I'm developing in iOS version 16.0 and I have an issue with refreshing scrollview within SwiftUI. I want to refresh and reload data for ScrollView from CoreData, and the ScrollView Code is written like this.

ScrollView {
    Spacer()
        .frame(height: 50)
    
    ForEach(viewModel.insightList, id: \.self) { insightData in
        InsightListCardView(viewModel: InsightListCardViewModel(insight: insightData))
    }
}
.refreshable {
    viewModel.getInsightList()
}

And the function getInsightList() is calling only one function, which is below.

func getAllInsights() -> [InsightData] {
    let fetchRequest: NSFetchRequest<InsightData> = InsightData.fetchRequest()
    let result = try? context.fetch(fetchRequest)
        
    return result?.reversed() ?? []        
}

And the problem is here. When I try to refresh the scrollview with pulling, the content of scrollview is moving down. The start position of scroll Indicator is also moving down, but not the entire scrollview height is decreased, so scrolling up is available.

The strange part is here. When I check the view hierarchy, only the screen is moved and the actual components are stay in the appropriate position. Buttons work not when I click the position on the screen, but when I click the original position.

simulator screenshots

2
  • You have to empty your insightList before refreshing. Commented Aug 21, 2023 at 10:31
  • I tried that, but emptying data doesn't help me. though, thank you for answering. Commented Aug 23, 2023 at 5:06

1 Answer 1

0

For a workaround you can try scrolling up as the

.refreshable

block triggers, by using

ScrollViewReader
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.