![](/img/trans.png)
[英]SwiftUI: List scrolls up on selection of a row when there's `.id(UUID())`
[英]List that scrolls to certain row when button pressed in SwiftUI app
在 SwiftUI 应用程序中,我试图创建一个在按下按钮时自动滚动到下一行的列表。
Apparently, this functionality isn't currently possible with a SwiftUI List, but is with UITableView using the scrollToRowAtIndexPath method as mentioned in part #3 of this answer https://stackoverflow.com/a/58830883/11698443 This answer is well explained but没有建议如何实施他们所说的最佳解决方案。
下面是一些示例代码,说明列表在 SwiftUI 中的外观。 目标是按下按钮,然后列表滚动到下一行。 我认为该解决方案需要与 UIKit 接口才能使其工作。 如果有人能弄清楚如何以最干净的方式做到这一点,那就太棒了。 不幸的是,这不能使用所有 SwiftUI 来完成。
提前感谢任何认为他们可以解决这个问题的人。
import SwiftUI
struct ScrollingList: View {
var body: some View {
List(0 ..< 10) { item in
VStack {
Text("\(item)")
Button(action: {}) {
Text("Scroll To Next Item")
}
}
.frame(height: 500)
.background(Color.blue)
}
}
}
查看这个漂亮的库https://github.com/siteline/SwiftUI-Introspect 。 有了它,你可以这样做:
import Introspect
List {
Text("some")
Text("list")
}.introspectTableView { tableView in
tableView.scrollToRowAtIndexPath(<#IndexPath#>, atScrollPosition: <#UITableView.ScrollPosition#>, animated: <#Bool#>)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.