I use LazyVStack because I need to load a big list of elements, LazyVStack helps with loading fewer elements, hence the scroll bar/indicator does not know the real size of the list, causing inconsistency on the scroll bar position, it stutters, meaning that the scroll goes up and down randomly, not knowing the real size of the list.
How can I change this, make the scroll bar consistent? I think its important to say that I know the real size of the list.
The code:
ScrollViewReader { proxy in
ScrollView {
LazyVStack {//this is the LazyVStack that cause the scroll bar to be inconsistent
ForEach(visibleCategories, id: \.self) { myCategory in
VStack {
let productsInCategory = productsByCategory[myCategory]
ForEach(productsInCategory, id: \.self.name) { product in
HStack{}.frame(width: UIScreen.main.bounds.width - 70, height: 105)
.padding(.trailing, 30)
Using simple VStack will solve the problem, but because I have a big list of elements, the performance will decrease, so I need to use LazyVStack.
The problem happens here:
LazyVStack{
ForEach(visibleCategories, id: \.self) { myCategory in
As there are more categories that the LazyVStack did not load.
How can I make the scroll consistent together with LazyVStack, knowing that I know the size of the list?
The
LazyVStackdoesn't load all the items at once and therefore doesn't know the full size of the scrollable content initially, which can cause the scroll indicator to behave inconsistently.I didn't test it, but you could you try this solution.
———
let contentHeight = topPadding(10) + bottomPaddding(10) + (itemSpacing * items.count) + (item.height * items.count)LazyVStackLazyVStack(spacing: 10) {}.frame(height: contentHeight)LazyVStackknows the correct height and it should display correct scroll indicator