There is very poor performance when pulling scroll bar indicator to bottom and scrolling cells with big ordinal numbers. But if we comment empty ViewModifier applied to cell, everything became ok.
This code is simplified example of real case, when number of items is big and we need an ability to recover scroll position to certain item (thats why we need id() and ScrollViewReader). So is it mean we are unable to use any ViewModifiers for cells?
final class ItemList { }
extension ItemList {
struct Ui : View {
@StateObject private var viewModel: ViewModel
init() { self._viewModel = StateObject(wrappedValue: ViewModel()) }
var body: some View {
ScrollView { ScrollViewReader { _ in
LazyVStack { ForEach(Array(viewModel.items.enumerated()), id: \.offset) {
Cell($0.offset)
.modifier(EmptyModifier()) // <- without one of these lines scroll performance is ok
.id($0.offset) // <- without one of these lines scroll performance is ok
} }.padding([.leading, .trailing])
} }
}
}
private struct Cell : View {
private let index: Int
init(_ index: Int) { self.index = index }
var body: some View {
HStack { Spacer(); Text("\(index)"); Spacer() }
.padding()
.background(Color.gray)
.cornerRadius(8)
}
}
private final class ViewModel : ObservableObject {
let items: [String] = .init(repeating: "item", count: 200000)
}
}
private struct EmptyModifier : ViewModifier {
func body(content: Content) -> some View { content }
}