【发布时间】:2020-04-01 15:28:12
【问题描述】:
我想请你帮忙。我正在尝试使用 geometry 捕获 VStack 高度,然后根据该 VStack 高度 值,计算其子元素的高度(在 VStack 内)。
Image of my current view of VStack
我在 VStack 之外使用 .frame 来填充整个屏幕。然后我用 .border 目视检查它是否真的填满了屏幕(它工作正常)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: Alignment.topLeading)
问题可以在 VStack 内部看到,高度显示为 GCFloat 734。尽管边框尺寸要大很多。
struct BodyView: View {
@State var stackHeight : CGFloat = 0
var body: some View {
GeometryReader { geometry in
VStack {
Text("VStack size: \(self.stackHeight)")
.bold()
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal)
.background(Color.green)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: Alignment.topLeading)
.onAppear{
self.stackHeight = geometry.size.height
}
.border(Color.purple, width: 5)
}
}
}
struct BodyView_Previews: PreviewProvider {
static var previews: some View {
BodyView()
}
}
如何在 VStack 完成加载后捕获它的实际大小?
当我触发(onAppear)时,VStack 高度显示正确,但是我需要立即捕获它。
【问题讨论】:
-
这能回答你的问题吗? Get CGRect of View
标签: ios swift swiftui screen vstack