我试图使用GeometryReader将maxHeight分配给Scrollview中的列表。
为了解决这个问题,我创建了以下项目:GeometryReaderTesting。
它使用以下参数设置ContentView:
@ViewBuilder将列表和最后文本提取到自己的视图中,并希望将列表的maxHeight设置为用户屏幕高度的.5。问题是,应用程序不会生成以下错误,而且GeometryReader似乎没有计算出正确的高度:

这是我的ContentView代码,如果有人知道我做错了什么…
struct ContentView: View {
let arrayWithStuff = [ "one","two","three","four","five","six","seven","eight","nine","ten", "eleven", "twelve"]
var heightOfView: CGFloat = 0.0 // To be set by GeometryReader
var body: some View {
let myString = "Top of Test App"
return ZStack {
GeometryReader { g in
heightOfView = g.size.height
NavigationView {
print("height of view : \(heightOfView)")
ScrollView {
VStack {
Text(myString)
.padding()
Divider()
self.ViewBody()
} // END of Vstack
.navigationBarTitle("Test App", displayMode: .inline)
} // END of Scrollview
}//End of NavigationView
} // End of Geometry reader
} // End of Zstack
} // End of body
@ViewBuilder func ViewBody() -> some View {
VStack {
List {
ForEach (self.arrayWithStuff, id: \.self) { item in
Text(item) }
} // END of List
.frame(maxHeight: heightOfView*0.5)
Divider()
Text("Bottom of TEST APP")
}
.padding()
}
}再一次,任何帮助都将不胜感激。
发布于 2020-05-28 09:57:36
下面是一个可能的解决方案的演示。用Xcode 11.4测试。

struct ContentView: View {
let arrayWithStuff = [ "one","two","three","four","five","six","seven","eight","nine","ten", "eleven", "twelve"]
var body: some View {
let myString = "Top of Test App"
return ZStack {
GeometryReader { g in
NavigationView {
ScrollView {
VStack {
Text(myString)
.padding()
Divider()
self.ViewBody(height: g.size.height)
} // END of Vstack
.navigationBarTitle("Test App", displayMode: .inline)
} // END of Scrollview
}//End of NavigationView
} // End of Geometry reader
} // End of Zstack
} // End of body
func ViewBody(height: CGFloat) -> some View {
print("height of view : \(height)")
return VStack {
List {
ForEach (self.arrayWithStuff, id: \.self) { item in
Text(item) }
} // END of List
.frame(height: height*0.5)
Divider()
Text("Bottom of TEST APP")
}
.padding()
}
}https://stackoverflow.com/questions/62061420
复制相似问题