SwiftUI VStack中大型Text和TextField之间的神秘间距

时间:2020-06-15 05:54:05

标签: swift swiftui

我很难弄清楚为什么我的文字下方有一些间距。

struct testView: View {

    @State private var notes = ""
    var body: some View {
        VStack {
            Text("Larg Text").font(.system(size: 70))
            .background(Color.red)

            TextField("Add a note", text: $notes)
            .background(Color.red)

            Spacer()
        }
        .background(Color.yellow)
    }
}

enter image description here

由于某种原因,在Text和TextField之间有一个神秘的空间。如果我

,这个空间似乎会减少
  • 减小字体大小
  • 不指定字体大小
  • 在“文本”视图后不要使用TextField
  • 在TextField之前不要使用Text视图

换句话说,字体大小相关的间距似乎仅发生在Text和TextField之间。我很困惑。我想摆脱这个空间。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

这是默认的自动间距。解决方案是明确指定

VStack(spacing: 0) {     // << here !!
    Text("Larg Text").font(.system(size: 70))
    .background(Color.red)

    TextField("Add a note", text: $notes)
    .background(Color.red)

    Spacer()
}