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"

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)
}
}
}

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

GeometryReaderfor 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)