admin管理员组

文章数量:1129450

I'm encountering an issue with inconsistent padding when using a Text view inside a VStack. When the Text is present, the padding becomes uneven, causing the horizontal and vertical padding to be inconsistent. However, when I remove the Text, the padding behaves as expected and remains consistent. I've found a workaround by adding a frame modifier to the Text, but I don't want to apply a frame in my UI design. Is there a way to resolve this issue without using the frame modifier?

struct ContentView: View {
    
    private let spacing: CGFloat = 20.0
    @State private var show: Bool = Bool()
    
    var body: some View {
        
        VStack(spacing: spacing) {
            
            if (show) {
                Text("Hello, world!")
                    .background(Color.blue)
            }
            
            Color.black
                .frame(width: 200.0, height: 200.0)
            
            Button("Reset") {
                show.toggle()
            }
            .buttonStyle(.borderedProminent)
        }
        .background(Color.red)
        .padding(spacing)
    }
}

本文标签: swiftInconsistent Padding in VStack with Text ViewStack Overflow