问题
My scrollview with vStack worked (in AppleTV - i did not test in iOS) without GeometryReader.
Then i added the geometryreader and the VStack "collapses" like a ZStack.
What can i do to solve this? (and yes, i need the geometryreader ;))
struct ContentView: View {
@State var offset : CGFloat = 0
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .leading) {
GeometryReader { geometry -> AnyView in
let newOffset = geometry.frame(in: .global).minY
if newOffset != self.offset {
self.offset = newOffset
print("new offset",newOffset)
}
return AnyView (
ForEach (0..<5) { _ in
Text("umpf")
}
)
}
}
}
}
}
Result:
and this code works as desired:
struct ContentView: View {
@State var offset : CGFloat = 0
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .leading) {
// GeometryReader { geometry -> AnyView in
// let newOffset = geometry.frame(in: .global).minY
// if newOffset != self.offset {
// self.offset = newOffset
// print("new offset",newOffset)
// }
// return AnyView (
ForEach (0..<5) { _ in
Text("umpf")
}
// )
// }
}
}
}
}
result:
回答1:
Let me suppose that initially you wanted this (and it does not break ScrollView layout & scrolling)
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .leading) {
ForEach (0..<5) { _ in
Text("umpf")
}
}.background(
GeometryReader { geometry -> Color in
let newOffset = geometry.frame(in: .global).minY
if newOffset != self.offset {
self.offset = newOffset
print("new offset",newOffset)
}
return Color.clear
}
)
}
}
来源:https://stackoverflow.com/questions/60965295/why-is-vstack-not-working-in-geometryreader-with-scrollview