当用户滚动到红色底部时,我试图不显示背景。我需要向下滚动时黄色部分始终接触底部。这是我的代码。
import SwiftUI
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
VStack {
Text("Content Above ScrollView")
.padding()
// ScrollView anchored to the bottom of the screen
ScrollView {
// Your ScrollView content
VStack(alignment: .leading) {
ForEach(0..<10) { index in
Text("Item \(index)")
}
Spacer()
}
.frame(maxWidth: .infinity)
.frame(height: geometry.size.height)
.background(.yellow)
}
.frame(maxHeight: .infinity) // Adjust height as per your requirement
}
.background(.red)
}
}
}
我尝试使用几何读取器来获取屏幕的高度并将其作为滚动视图的高度。
那就是
safeAreaBottom
,你需要忽略与外部VStack的间距:
GeometryReader { geometry in
VStack {
...
}
.ignoresSafeArea(.container, edges: .bottom) //<- here
.background(.red)
}