SwiftUI 坐标空间 [英] SwiftUI coordinate space
问题描述
有谁知道 SwiftUI 在坐标空间方面是如何渲染的?看起来框架的原点现在不在左上角的 0, 0
处.例如,添加带有修饰符的 Text
会将标签偏移到视图之外.
Does anyone know how does SwiftUI render in terms of coordinate space? It doesn't look like the frame's origin is now at the 0, 0
in the left top corner. For example adding a Text
with a modifier would offset the label outside of a view.
var body: some View {
Text("my long enough string")
.position(CGPoint(x: 0, y: 100))
}
结果是我只能看到呃字符串".
The result is I can only see "ugh string".
推荐答案
.position(x, y) 将视图的中心定位到父级的指定坐标处.
.position(x, y) will position the center of the view to the specified coordinate with the parent.
您将需要使用堆栈/容器使用对齐和间隔来相对于彼此定位显示的元素.
You will need to use a Stacks/Container to position the displayed elements relative to each other using aligments and spacer.
视图总是从屏幕中央开始.
Views always start out at the center of the screen.
像这样:
HStack(alignment: .top) {
VStack(alignment: .leading) {
Text("top left")
Spacer()
Text("bottom left")
}
Spacer()
VStack(alignment: .leading) {
Text("top right")
Spacer()
Text("bottom right")
}
}
这篇关于SwiftUI 坐标空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!