iOS 14 添加了一个新ScrollViewReader
视图,您可以将其添加到视图层次结构中,ScrollView
或者List
可以通过 滚动到特定视图id
。但是,当您的部署目标是 iOS 13 时,如何添加它,而不用 包装它来复制视图层次结构if #available
?
List {
ScrollViewReader { reader in //FIXME: 'ScrollViewReader' is only available in iOS 14.0 or newer
Section(header:
Text("Header")
.id(0)
) {
ForEach(items) { item in
ItemCell()
}
}
//for an example
if #available(iOS 14.0, *) {
Button("Scroll to Top") {
reader.scrollTo(0)
}
}
}
}