\n
struct ContentView: View {\n \n init() { UITableView.appearance().backgroundColor = UIColor.clear }\n \n var body: some View {\n NavigationView {\n ZStack {\n Color.red\n .ignoresSafeArea()\n VStack {\n HStack {\n Text("Faux Title")\n .font(.system(.largeTitle, design: .rounded))\n .fontWeight(.heavy)\n Spacer()\n Button(action: {\n // settings\n }, label: {\n Image(systemName: "gearshape.fill")\n .font(.system(.title2))\n })\n }\n .padding()\n \n GeometryReader { geometry in\n HStack() {\n Text("1")\n .frame(width: geometry.size.width * 0.30, height: 150)\n .background(Color.blue)\n Spacer()\n Text("2")\n .frame(width: geometry.size.width * 0.30, height: 150)\n .background(Color.blue)\n Spacer()\n Text("3")\n .frame(width: geometry.size.width * 0.30, height: 150)\n .background(Color.blue)\n }\n }\n .padding()\n \n List {\n Text("One")\n Text("Two")\n Text("Three")\n Text("Four")\n Text("Five")\n Text("Six")\n }\n .listStyle(InsetGroupedListStyle())\n }\n }\n .navigationBarHidden(true)\n }\n }\n}\n
\nBonus 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?
\n","author":{"@type":"Person","name":"Sam"},"upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"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\n ...\n}\n.padding()\n.frame(height: 150) //Here\n
\nOtherwise, 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
\n","author":{"@type":"Person","name":"jnpdx"},"upvoteCount":1}}}Reputation: 2565
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.
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?
Upvotes: 1
Views: 4564
Reputation: 52615
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
Upvotes: 1
Reputation: 2408
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
Upvotes: 5