SwiftUI ScrollView сохраняет позицию при загрузке новой страницыIOS

Программируем под IOS
Anonymous
SwiftUI ScrollView сохраняет позицию при загрузке новой страницы

Сообщение Anonymous »

В моем представлении чата каждый раз, когда я загружаю новую страницу (элементы добавляются сверху), ScrollView переходит наверх вместо сохранения ScrollPosition.
Изображение
Вот мой вид прокрутки:

Код: Выделить всё

GeometryReader { geometryProxy in
ScrollView(showsIndicators: false) {
VStack(spacing: 0) {
if viewModel.isLoading {
LoadingFooter()
}

messagesView
.frame(minHeight: geometryProxy.size.height - loadingFooterHeight - bottomContentMargins, alignment: .bottom)
}
}
.scrollDismissesKeyboard(.interactively)
.defaultScrollAnchor(.bottom)
.scrollPosition(id: $scrolledId, anchor: .top)
.contentMargins(.bottom, bottomContentMargins, for: .scrollContent)
.onChange(of: scrolledId, scrollViewDidScroll)
}
А это вид сообщений

Код: Выделить всё

@ViewBuilder var messagesView: some View {
LazyVStack(spacing: 0) {
ForEach(sectionedMessages) { section in
Section(header: sectionHeaderView(title: section.id)) {
ForEach(section, id: \.id) { message in
MessageView(message: message)
.padding(.horizontal, .padding16)
.padding(.bottom, .padding8)
.id(message.id)
}
}
}
.scrollTargetLayout()
}
}
Распечатывая прокрутку после загрузки страницы, я вижу, что он не изменился, но позиция ScrollView изменилась.

Подробнее здесь: https://stackoverflow.com/questions/773 ... -page-load

Вернуться в «IOS»