swiftui - 如何限制 VStack 中 GeometryReader 的大小
问题描述
我想停止GeometryReader
干扰我的布局,VStack
但我不确定正确的方法是什么。
给定一个带有标题和标题的简单图形示例:
struct Graph: View {
var body: some View {
HStack(spacing: 0) {
Color.red.frame(width: 100, height: 80)
Color.blue.frame(width: 100, height: 120)
Color.green.frame(width: 100, height: 180)
}
}
}
struct GraphExample: View {
var body: some View {
VStack {
Text("My graph")
// Graph represents a custom view with a natural and non-static height
Graph()
Text("My graph caption")
}
}
}
产生这个预期的结果:
但是,如果我们更新Graph
视图以使用 aGeometryReader
在屏幕宽度上均匀分割图形,则布局会发生变化。
struct Graph: View {
var body: some View {
GeometryReader { proxy in
HStack(spacing: 0) {
Color.red.frame(width: proxy.size.width / 3, height: 80)
Color.blue.frame(width: proxy.size.width / 3, height: 120)
Color.green.frame(width: proxy.size.width / 3, height: 180)
}
}
}
}
这使得Graph
视图填充了所有可用空间VStack
有没有办法让Graph
视图只填充其自然高度,并且在仍然使用GeometryReader
.
请记住,Graph
此处可能是任何不知道确切大小的视图,因此无法设置静态大小。即没有GeometryReader
, Graph
can 只占用 VStack 中所需的垂直空间量。
解决方案
这是可能的解决方案。使用 Xcode 11.4 / iOS 13.4 测试
struct Graph: View {
@State private var width = UIScreen.main.bounds.width // just initial constant
var body: some View {
HStack(spacing: 0) {
Color.red.frame(width: width / 3, height: 80)
Color.blue.frame(width: width / 3, height: 120)
Color.green.frame(width: width / 3, height: 180)
}.background(GeometryReader { gp -> Color in
let frame = gp.frame(in: .local)
DispatchQueue.main.async {
self.width = frame.size.width // << dynamic, on layout !!
}
return Color.clear
})
}
}
推荐阅读
- c# - 如何返回主菜单并将列表分列?
- r - 根据恒定增长因子计算每月人口估计值
- python - 动态编程背包
- c# - 在 DotVVM 的自定义标记控件中呈现 ITemplate
- python - 如何在电子生成器构建中包含 python 文件?
- arrays - 从 http 响应中解组动态 JSON
- javascript - javascript quiz:使所有最初一次出现的问题出现在测验的末尾
- reactjs - Next.js:下拉菜单中的链接不起作用
- amazon-web-services - 使用 Fargate 时如何增加 AWS EKS 中的节点数?
- python - Heroku生产环境中的Django TemplateDoesNotExist错误但在本地开发环境中没有