【发布时间】:2020-11-23 11:11:55
【问题描述】:
我刚刚开始在名为 creating and combining views 的网站上关注 Apple 的 SwiftUI 课程。
我注意到 ContentView 结构中显示的代码中有一个我无法理解的奇怪行为,这是代码和结果的图像。
struct ContentView: View {
var body: some View {
VStack {
MapView()
.edgesIgnoringSafeArea(.top)
.frame(height: 300)
CircleImage()
.offset(y : -130)
.padding(.bottom, -130)
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
Spacer()
}
}
}
这里特别注意,但是将 MapView 的修饰符的顺序更改为时。
MapView()
.frame(height: 300)
.edgesIgnoringSafeArea(.top)
circleView 的中心不再位于 mapView 的底部,见图。
我注意到当我删除底部的 Spacer() 时不会发生这种情况,
那么,这里到底发生了什么?为什么修饰符的顺序很重要? Spacer() 如何以这种方式影响其他视图?
感谢您的帮助。
【问题讨论】:
-
这就是为什么:Why modifier order matters。因为您正在创建一个框架而没有忽略第二个代码上的安全区域。