下面的图片是我写的代码的输出。当把文字和我的图片放在ZStack中时,文字会自动居中。但我希望文字能被放置在Y轴上的100号之上。我如何才能做到这一点?图片
声明:我使用的是Swift Playground
这是我的代码
import SwiftUI
import PlaygroundSupport
struct ContentView : View {
var body: some View {
ZStack {
Image(uiImage: UIImage(named: "Screenshot 2020-05-12 at 14.14.22")!).resizable()
.frame(width: 700.0, height: 700.0)
Text("Hello")
.foregroundColor(Color.black)
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
以下是可能的方法
ZStack {
Image(uiImage: UIImage(named: "large_image")!).resizable()
.frame(width: 700.0, height: 700.0)
.overlay(GeometryReader { gp in
Text("Hello")
.foregroundColor(Color.black)
.position(x: gp.size.width / 2, y: gp.size.height / 4)
}
)
}
当然,如果你生成图像,那么你应该知道更精确的y:坐标来定位文本。GeometryReader在这种情况下给出了图像的局部坐标空间。