【发布时间】:2021-05-19 21:57:42
【问题描述】:
谁能解释一下原因:
GeometryReader 将其内容定位在左上角,而不是中心。
GeometryReader 占用所有可用空间。
struct ContentView: View {
var body: some View {
VStack(alignment: .center) {
GeometryReader { geo in
Text("Hello, World!")
.frame(width: geo.size.width * 0.7, height: 40)
.background(Color.red)
}
.background(Color.yellow)
Text("More text")
.background(Color.blue)
}
}
}
看起来是这样的:
我希望它将Text“Hello, World”视图定位在中心。
【问题讨论】:
-
GeometryReader 占用所有可用空间。这是有意的,请阅读hackingwithswift.com/books/ios-swiftui/…
-
这篇文章解释了“获取所有可用空间”,但没有关于对齐的线索,@vadian。