问题
How can I make a SwiftUI List
start scrolling from the bottom of the screen (like a chat view)?
Ideally, I want to mimic, e.g. the behavior of iMessage when the list updates, meaning it shifts down if an item is added when the user is at the bottom, but holds it’s position if the user manually scrolled up.
The list is read directly from a binding array, and the order can be reversed if convenient.
@komal pointed out that the
UITableView
(the backend ofList
) has anatScrollPosition
that should provide this functionality. However, there doesn't seem to be a way to access the underlying view without completely reimplementingList
as aUIViewRepresentable
, which is easier said than done, considering the standard implementation is completely black-boxed and closed-source.With that said, I've also posted Access underlying UITableView from SwiftUI List, which, if solved, could serve as an answer to this question.
回答1:
I would suggest that instead of using scrollview, use UITableView as Tablview inherits scroll property of UIScrollview. And you can use "atScrollPosition:UITableViewScrollPositionBottom" to achieve this behavior.
来源:https://stackoverflow.com/questions/58101865/bottom-first-scrolling-in-swiftui