gpt4 book ai didi

SwiftUI 在图像周围生成不需要的空间

转载 作者:行者123 更新时间:2023-12-03 21:17:03 25 4
gpt4 key购买 nike

下面的代码生成一个简单的 VStack 和 TextView ,它们之间没有显示间距(第 1 行和第 2 行)。

但是,将图像添加到第三行(绿色)会在整行的上方和下方增加不需要的间距。

struct ContentView: View {
var body: some View {
VStack {
HStack {
Text("one thing")
}.background(Color(.yellow))
HStack {
Text("nothing")
}.background(Color(.red))
HStack {
Text("three")
Image(systemName: "star")
.resizable()
.frame(width: 8, height: 8)
}.background(Color(.green))
HStack {
Text("three things")
}.background(Color(.red))
}
}
}

如何避免额外的不需要的空间?

空间显示与图像大小无关(即使图像尺寸只有几个像素)。

而且,当然,我想知道为什么会生成空间。

谢谢你的帮助

以上代码截图:

Output:

最佳答案

您可以调整 VStack 的间距:

var body: some View {
VStack (spacing: 0) {
HStack {
Text("one thing")
}.background(Color(.yellow))
HStack {
Text("nothing")
}.background(Color(.red))
HStack {
Text("three")
Image(systemName: "star")
.resizable()
.frame(width: 8, height: 8)
}.background(Color(.green))
HStack {
Text("three things")
}.background(Color(.red))
}
}

关于SwiftUI 在图像周围生成不需要的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com