【发布时间】:2020-10-25 11:28:41
【问题描述】:
我在玩 .position(x:y:) 和 CoordinateSpace.global 时遇到了一些让我感到困惑的事情。
我试图确定 ZStack 在全局坐标空间中的原点坐标。但是,当我在这些坐标上放置一个点时,它并没有与 ZStack 的左上角对齐。
这是我使用的代码:
struct Test: View {
var body: some View {
ZStack {
Image(systemName: "circle.fill")
.resizable()
.frame(width: 5, height: 5)
.foregroundColor(.red)
.position(x: 177, y: 423) // This is from 'frame.origin.x' and 'frame.origin.y'
ZStack { // Trying to determine this ZStack's coordinates in the global coordinate space
GeometryReader { geometry -> AnyView in // Used to retrieve the coordinates using `geometry` and then returning a Text so I can see the coordinates on screen
let frame = geometry.frame(in: CoordinateSpace.global)
return AnyView(Text("\(frame.origin.x), \(frame.origin.y)").fixedSize(horizontal: false, vertical: true))
}
}
.frame(width: 60, height: 60)
}
}
}
有谁知道它为什么出现在应该出现在 ZStack 左上角的那个奇怪的地方?我以为原点应该在视图的左上角?
【问题讨论】:
-
不清楚你想达到什么目的?你会详细说明吗?您要放置哪个视图和位置。
-
@Asperi 这样更好吗?我试图确定 ZStack 的起源
标签: ios swift layout swiftui frame