我有这个简单的 SwiftUI 代码。我希望所有符号都居中对齐,就像云符号一样。
struct ContentView : View {
var body: some View {
HStack(alignment: .center, spacing: 10.0) {
Image(systemName: "cloud.sun")
Image(systemName: "cloud")
Image(systemName: "cloud.bolt")
Text("Text")
}.font(.title)
}
}
但正如您在下面看到的,第一个和最后一个符号不居中。我错过了什么,还是这是一个错误?
Cheers!
这就是正在发生的事情。
The Image
视图没有调整大小。
他们似乎不知道其内在内容的大小,或者可能报告了错误的值。
要解决这个问题:
struct ContentView : View {
var body: some View {
HStack(alignment: .center, spacing: 10.0) {
Image(systemName: "cloud.sun")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.red)
Image(systemName: "cloud")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.yellow)
Image(systemName: "cloud.bolt")
.resizable()
.aspectRatio(contentMode: .fit)
.background(Color.pink)
Text("Text").background(Color.green)
}
.frame(width: 250, height: 50)
.background(Color.gray)
.font(.title)
}
}
...使Images
可调整大小,并确保宽高比设置为.fit
,否则它们会拉伸。
还设置框架HStack
或者它会扩展以填满整个屏幕。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)