SwiftUI 中的底部优先滚动 [英] Bottom-first scrolling in SwiftUI

查看:35
本文介绍了SwiftUI 中的底部优先滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 SwiftUI List 从屏幕底部开始滚动(如聊天视图)?

How can I make a SwiftUI List start scrolling from the bottom of the screen (like a chat view)?

理想情况下,我想模仿,例如当列表更新时 iMessage 的行为,这意味着如果用户在底部时添加了一个项目,它会向下移动,但如果用户手动向上滚动,它会保持它的位置.

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 指出 UITableView(List 的后端)有一个 atScrollPosition 应该提供这个功能.然而,似乎没有一种方法可以在不完全将 List 重新实现为 UIViewRepresentable 的情况下访问底层视图,这说起来容易做起来难,考虑到标准实现是完全黑盒化和闭源.

@komal pointed out that the UITableView (the backend of List) has an atScrollPosition that should provide this functionality. However, there doesn't seem to be a way to access the underlying view without completely reimplementing List as a UIViewRepresentable, which is easier said than done, considering the standard implementation is completely black-boxed and closed-source.

话虽如此,我还发布了从 SwiftUI 列表访问底层 UITableView,如果解决了,可以作为这个问题的答案.

With that said, I've also posted Access underlying UITableView from SwiftUI List, which, if solved, could serve as an answer to this question.

推荐答案

我建议不要使用滚动视图,而是使用 UITableView,因为 Tablview 继承了 UIScrollview 的滚动属性.您可以使用atScrollPosition:UITableViewScrollPositionBottom"来实现此行为.

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.

这篇关于SwiftUI 中的底部优先滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆