【发布时间】:2020-08-25 03:10:58
【问题描述】:
如果您在 ScrollView 中展示一个在下方显示带有文本的图像的工作表,当您向下滚动然后向上滑动时,请注意它会滚动超出限制并且图像上方会出现空白空间,直到弹性滚动效果稳定.然后,您可以向下拉以关闭工作表。
我想防止在 ScrollView 中的图像上方添加空间。相反,我希望该填充出现在图像和第一个文本之间。执行此操作的应用是 App Store - 当您在 Today 中点击故事时,请注意顶部的图像在向上滚动超过限制时始终保持固定在顶部,并且在其下方出现弹性反弹效果。
我怀疑可以利用 GeometryReader 来获取全局 CoordinateSpace 中的位置,但我不清楚如何利用它来获得所需的行为。
struct ContentView: View {
@State private var sheetVisible = false
var body: some View {
VStack {
Button(action: {
sheetVisible = true
}, label: {
Text("Present Sheet")
})
}
.sheet(isPresented: $sheetVisible) {
DetailsView(image: UIImage(named: "test"))
}
}
}
struct DetailsView: View {
var image: UIImage?
var body: some View {
ScrollView {
Image(uiImage: image ?? UIImage())
.resizable()
.aspectRatio((image?.size.width ?? 1) / (image?.size.height ?? 1), contentMode: .fill)
.background(Color(.secondarySystemFill))
ForEach(0..<50) { index in
Text("\(index)")
}
}
}
}
【问题讨论】:
-
您不想按照stackoverflow.com/a/63322713/12299030 中的说明禁用退回吗?
-
虽然这会解决不良行为,但仍需要支持弹性滚动行为,而不是在顶部突然停止滚动