I've found that using scrollTo inside of an onChange closure doesn't scroll to the correct location if the row height of the List item is greater than a single row. It will scroll to the height of a single row leaving everything else in the row hidden. If I use the scrollTo inside a Button action it will accurately read the height of the VStack and scroll to a position that shows the full row.
The second image is a result of clicking the button, the first is the triggering of the onChange event.
Here is the code I'm working with:
Button("Scroll To ") {
readerProxy?.scrollTo(careLogItems.last)
}
ScrollViewReader { reader in
List(careLogItems) { i in
VStack {
Text(i.logText)
Text(i.logDate.description)
}.id(i)
}.onAppear {
readerProxy = reader
//reader.scrollTo(careLogItems.last)
}
CareLogItemDetail(careLogItem: $newCareLogItem, addItem: addCareLogItem)
}
}
.onChange(of: careLogItems) { _ in
print("changed \(careLogItems.count)")
readerProxy?.scrollTo(careLogItems.last)
}
.onAppear {
if (hasAppeared == false) {
hasAppeared = true
loadCareLogItems()
}
}
![]("https://developer.apple.com/forums/content/attachment/d409d385-9415-47cf-ad0a-aa9c7f317ac9" "title=Screenshot 2023-07-25 at 8.26.25 AM.png;width=548;height=1160")
![]("https://developer.apple.com/forums/content/attachment/54325855-d6c5-4aee-a817-2a7059c872d5" "title=Screenshot 2023-07-25 at 8.26.59 AM.png;width=536;height=1176")