1

Why is there so much space between the three blue rectangles and the list? How can I remove the space so that all views within the VStack stack at the top? I tried using a Spacer() directly after the List, but nothing changed.

spacing

struct ContentView: View {
    
    init() { UITableView.appearance().backgroundColor = UIColor.clear }
    
    var body: some View {
        NavigationView {
            ZStack {
                Color.red
                    .ignoresSafeArea()
                VStack {
                    HStack {
                        Text("Faux Title")
                            .font(.system(.largeTitle, design: .rounded))
                            .fontWeight(.heavy)
                        Spacer()
                        Button(action: {
                            // settings
                        }, label: {
                            Image(systemName: "gearshape.fill")
                                .font(.system(.title2))
                        })
                    }
                    .padding()
                    
                    GeometryReader { geometry in
                        HStack() {
                            Text("1")
                                .frame(width: geometry.size.width * 0.30, height: 150)
                                .background(Color.blue)
                            Spacer()
                            Text("2")
                                .frame(width: geometry.size.width * 0.30, height: 150)
                                .background(Color.blue)
                            Spacer()
                            Text("3")
                                .frame(width: geometry.size.width * 0.30, height: 150)
                                .background(Color.blue)
                        }
                    }
                    .padding()
                    
                    List {
                        Text("One")
                        Text("Two")
                        Text("Three")
                        Text("Four")
                        Text("Five")
                        Text("Six")
                    }
                    .listStyle(InsetGroupedListStyle())
                }
            }
            .navigationBarHidden(true)
        }
    }
}

Bonus question: In web development, you can open your browser's Web Inspector and use the element selector to click on elements which highlights their borders. Useful for something like this where you're trying to figure out which element the offending spacing belongs to. Is there something like that in Xcode?

0

2 Answers 2

5
VStack(spacing: 0) {...} 
Spacer()

to your question you can in Xcode use the view inspector. https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html

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

1 Comment

This didn't work, unfortunately. But thanks for the heads-up about the View Hierarchy!
1

Since you know that your HStack with the blue rectangles is going to be a height of 150, you should constrain it to that using .frame(height: 150):

GeometryReader { geometry in
    ...
}
.padding()
.frame(height: 150) //Here

Otherwise, the GeometryReader will occupy all available vertical space.

Re: your web dev comparison, check out the Xcode view hierarchy inspector. It's not exactly the same, but it's in the same vein: https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html

1 Comment

Yes! This fixes it! Thank you. And your tip about the View Hierarchy is pure gold.

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.