【问题标题】:SwiftUI mysterious spacing between large Text and TextField in VStackVStack中大文本和TextField之间的SwiftUI神秘间距
【发布时间】:2020-06-15 05:54:05
【问题描述】:

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

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)
    }
}

出于某种原因,Text 和 TextField 之间有一个神秘的空间。如果我这样做,这个空间似乎会减少

  • 减小字体大小
  • 不要指定字体大小
  • 不要在文本视图之后使用 TextField
  • 不要在 TextField 之前使用 Text 视图

换句话说,这种与字体大小相关的间距似乎只发生在 Text 和 TextField 之间。我完全糊涂了。我想摆脱这个空间。

感谢您的帮助!

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

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

    VStack(spacing: 0) {     // << here !!
        Text("Larg Text").font(.system(size: 70))
        .background(Color.red)
    
        TextField("Add a note", text: $notes)
        .background(Color.red)
    
        Spacer()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-16
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 2012-06-25
      • 2021-03-31
      • 2021-10-24
      相关资源
      最近更新 更多