1

My code is as bellow :

import SwiftUI


struct DotView: View {
    var body: some View {
        GeometryReader { geo in
            let width = geo.size.width
            VStack() {
                Circle()
                    .frame(width: width, height: width)
                Spacer()
                Circle()
                    .frame(width: width, height: width)
            }
        }
    }
}

struct TestView: View {
    var body: some View {
        GeometryReader { geo in
            VStack() {
                HStack() {
                    Text("11")
                    DotView()
                        .frame(width: 8, height: 23, alignment: .center)
                    Text("22")
                }
                .font(.system(size: 48))
                .background(Color.gray)
                HStack() {
                    Text("123456789")
                }
                .font(.system(size: 30))
                .background(Color.gray)
            }
            .frame(width: geo.size.width, height: geo.size.height)
            .background(Color.blue)
        }
    }
    
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
            .frame(width: 200, height: 200, alignment: .center)
            .cornerRadius(23.0)
    }
}

The view is as bellow picture, there is a spacing between "11:22" and "123456789"

Text

However, when I comment the view DotView as bellow code:

struct TestView: View {
    var body: some View {
        GeometryReader { geo in
            VStack() {
                HStack() {
                    Text("11")
//                    DotView()
//                        .frame(width: 8, height: 23, alignment: .center)
                    Text("22")
                }
                .font(.system(size: 48))
                .background(Color.gray)
                HStack() {
                    Text("123456789")
                }
                .font(.system(size: 30))
                .background(Color.gray)
            }
            .frame(width: geo.size.width, height: geo.size.height)
            .background(Color.blue)
        }
    }
}

Text

The spacing disappear. Why this DotView will affect the spacing?

1
  • Try avoiding GeometryReader for simple layouts like this. Only use it when you need to get the width/height, and use it to calculate something else (e.g. a progress bar whose width is 70% of total) Commented Jun 13, 2021 at 18:57

1 Answer 1

2

You can add "(spacing: 0)" to VStack in TestView

VStack(spacing: 0)

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I know spacing 0 can solve it. But why default spacing will lead to this gap?
@mars The definition of VStack says "spacing: The distance between adjacent subviews, or nil if you want the stack to choose a default distance for each pair of subviews". Therefore, if spacing is not provided in VStack, the default distance of each subview(e.g. HStack(), Text() and DotView() in VStack) is entered. And in this case, when calling a struct that conforms to the View protocol called DotView() struct, the default distance is applied, so you have to adjust the spacing in VStack, if you want adjust the spacing of each subviews.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.