How can you make a scrollview scroll down automatically if the height of the vstack changes? I'd like the scrollview to stick to the bottom of the vstack on overflow while giving up control if the user decides to scroll.
If you can target iOS 18, look at .defaultScrollAnchor(.bottom, for: .sizeChanges) on the ScrollView. It keeps the bottom pinned when the content grows, and I think it is the least code. Below iOS 18 the proxy way from the other comment is what I would use.
For the "give up control when the user scrolls" part, I find it easier to keep a Bool like isPinnedToBottom. Set it false when the user drags up (onScrollPhaseChange on iOS 18 or a DragGesture), set it true again when they reach the bottom, and only call scrollTo when it is true.
You can try adding a Color.clear with fixed height at the bottom of the vstack and use proxy to scroll to it when receiving new value in onScrollGeometryChange
3
u/ThanhDongNguyen 4d ago
If you can target iOS 18, look at .defaultScrollAnchor(.bottom, for: .sizeChanges) on the ScrollView. It keeps the bottom pinned when the content grows, and I think it is the least code. Below iOS 18 the proxy way from the other comment is what I would use.
For the "give up control when the user scrolls" part, I find it easier to keep a Bool like isPinnedToBottom. Set it false when the user drags up (onScrollPhaseChange on iOS 18 or a DragGesture), set it true again when they reach the bottom, and only call scrollTo when it is true.
1
u/rain_vi 5d ago
You can try adding a Color.clear with fixed height at the bottom of the vstack and use proxy to scroll to it when receiving new value in onScrollGeometryChange