SwiftUI padding horizontally gives unexpected gap

Viewed 575

I have the following view:

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack{
            Color.green.edgesIgnoringSafeArea(.all)
            VStack {
                HStack{
                    Text("header leading")
                    Spacer()
                    Text("header trailing")
                }
                // this makes the unexpected gap somehow
                .padding(.horizontal)
                .frame(height: 60)
                .background(Color.red)

                VStack{
                    Text("body top")
                    Spacer()
                    Text("body bottom")
                }
                .background(Color.blue)
            }
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Why do I get this gap between the red HStack and the blue VStack? If I remove padding, the gap is gone.

For my understanding, the padding should only make in indent horizontally (leading and trailing). Why is this assumption wrong?

enter image description here

1 Answers
import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack{
            Color.green.edgesIgnoringSafeArea(.all)
            VStack(spacing:0.0) {
                HStack{
                    Text("header leading")
                    Spacer()
                    Text("header trailing")
                }
                // this makes the unexpected gap somehow
                .padding(.horizontal)
                .frame(height: 60)
                .background(Color.red)

                VStack{
                    Text("body top")
                    Spacer()
                    Text("body bottom")
                }
                .background(Color.blue)
            }
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
[screen capture in simulator iphone 8][1]
Related