我为 answer 上的 ScrollView 案例提供了一个可重用的解决方案,它使用 View Preferences 作为在 View Hierarchy 中通知上游布局信息的方法。
关于View Preferences如何工作的详细解释,我建议阅读3 articles serieskontiki的主题
不幸的是,这种解决方案不适用于 List(可能是一个错误),因为 View Preferences 被困在 List 中并且其祖先不可见。
目前唯一可行的解决方案是观察列表内视图的框架变化。您可以通过两种方式实现这一目标:
您可以报告和收听列表中每个视图(单元格)的布局更改(并对其采取行动):
struct TestView1: View {
var body: some View {
GeometryReader { geometry in
List(TestEnum.allCases) { listValue in
Text(listValue.id)
.padding(60)
.transformAnchorPreference(key: MyKey.self, value: .bounds) {
$0.append(MyFrame(id: listValue.id, frame: geometry[$1]))
}
.onPreferenceChange(MyKey.self) {
print($0)
// Handle content frame changes here
}
}
}
}
}
或者,如果您不需要每个单元格上的框架更改,则报告并收听某些表格标题视图(或空标题)上的框架更改:
struct TestView2: View {
var body: some View {
GeometryReader { geometry in
List {
Text("")
.transformAnchorPreference(key: MyKey.self, value: .bounds) {
$0.append(MyFrame(id: "tableTopCell", frame: geometry[$1]))
}
.onPreferenceChange(MyKey.self) {
print($0)
// Handle top view frame changes here.
// This only gets reported as long as this
// top view is part of the content. This could be
// removed when not visible by the List internals.
}
ForEach(TestEnum.allCases) {
Text($0.rawValue)
.padding(60)
}
}
}
}
}
在下面找到上述解决方案的支持代码:PreferenceKey 符合结构,一个可识别的视图框架结构和一个测试枚举作为数据源:
struct MyFrame : Equatable {
let id : String
let frame : CGRect
static func == (lhs: MyFrame, rhs: MyFrame) -> Bool {
lhs.id == rhs.id && lhs.frame == rhs.frame
}
}
struct MyKey : PreferenceKey {
typealias Value = [MyFrame] // The list of view frame changes in a View tree.
static var defaultValue: [MyFrame] = []
/// When traversing the view tree, Swift UI will use this function to collect all view frame changes.
static func reduce(value: inout [MyFrame], nextValue: () -> [MyFrame]) {
value.append(contentsOf: nextValue())
}
}
enum TestEnum : String, CaseIterable, Identifiable {
case one, two, three, four, five, six, seven, eight, nine, ten
var id: String {
rawValue
}
}