This is my code to create HStack
with equal width elements for full width parent View
:
HStack(alignment: .center, spacing: 0) {
ForEach(0...6, id: .self) { _ in
Color(UIColor.white.withAlphaComponent(0.1))
.cornerRadius(5)
.padding(2)
.frame(minWidth: 0, maxWidth: .infinity)
.frame(minHeight: 0, maxHeight: .infinity)
}
}
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: 70)
and the effect is:
But now I need to add the center "T"
text inside every View
at the top, so I simply replace Color
view with VStack
view and:
HStack(alignment: .center, spacing: 0) {
ForEach(0...6, id: .self) { _ in
VStack(alignment: .center) {
Text("T")
}
.background(Color(UIColor.white.withAlphaComponent(0.1)))
.cornerRadius(5)
.padding(2)
.frame(minWidth: 0, maxWidth: .infinity)
.frame(minHeight: 0, maxHeight: .infinity)
}
}
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: 70)
and the effect is wrong. Why?
What to do to keep size of the rectangle views with "T"
, as it was without "T"
?
2
Answers
I think you want
scaledToFill()
on both theVStack
and theHStack
:And where the
.frame()
is set does make a difference as pointed out by @timbreHere you want to use an overlay for what your are trying to achieve:
Yout don’t need
.frame(minWidth: 0, maxWidth: .infinity)
modifier sinceColor
by default already takes up all the space available. If you don’t want any color in the background set it to.clear
.