SwiftUI:在文本之间添加仅 1 的填充会创建一个巨大的填充 - 如何实际制作一个小的填充?

Tom*_*fka 4 swiftui

我有Textsinside VStack,我想在它们之间添加一些空白。

但是,设置最小可能的填充 (= 1) 会创建太大的填充,而填充为零则根本不会创建任何填充。

SwiftUI 似乎以某种方式计算realPadding = declaredPadding > 0 ? declaredPadding + 8 : 0,这使得较小的填充无法实现。

为什么会发生这种情况?这种行为实际上记录在某处吗?

现在看来,如果我想要例如。文本之间有2pt的差距,我运气不好。

有没有办法真正让文本视图之间的填充比我得到的更小.padding(.bottom, 1)

Nikita Tonsky批评了这种“自动”行为,但他没有提出解决方案。

要粘贴到游乐场的示例代码:

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
        var body: some View {
            VStack {
                VStack{
                    Text("Hello World")
                        .background(Color.red)
                        .padding(.bottom, 0)
                    Text("padding 0")
                        .background(Color.red)
                }
                .padding()
                
                VStack{
                    Text("Hello World")
                        .background(Color.red)
                        .padding(.bottom, 1)
                    Text("padding 1 - a huge step-up from 0, why?")
                        .background(Color.red)
                }
                .padding()
                
                VStack{
                    Text("Hello World")
                        .background(Color.red)
                        .padding(.bottom, 2)
                    Text("padding 2 - just 1 pt larger than 1")
                        .background(Color.red)
                }
                .padding()
            }
        }
}

PlaygroundPage.current.setLiveView(ContentView())
Run Code Online (Sandbox Code Playgroud)

外观如何:

SwiftUI 填充错误?

Tae*_*Kim 7

如果添加(spacing: 0)到 VStack,则填充会恰好添加 1。

VStack(spacing: 0) { // here
    Text("Hello World")
        .background(Color.red)
        .padding(.bottom, 1)
    Text("padding 1 - a huge step-up from 0, why?")
        .background(Color.red)
}
.padding()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述