Reputation: 4308
I want the Text("111")
to have the equal height of the VStack
containing 2222... and 333....
struct Test7: View {
var body: some View
{ HStack (alignment: .top) {
Text( "111") // Shall have equal Height
.background(Color.red)
VStack(alignment: .leading){. // of this VStack
Text("2222222")
.background(Color.gray)
Text("333333333")
.background(Color.blue)
}
}
.background(Color.yellow)}
}
I tried with a GeometryReader
but didn't get it to work
Upvotes: 5
Views: 7623
Reputation: 257513
Here is possible approach using .alignmentGuide
struct Test7: View {
@State
private var height: CGFloat = .zero // < calculable height
var body: some View {
HStack (alignment: .top) {
Text( "111")
.frame(minHeight: height) // in Preview default is visible
.background(Color.red)
VStack(alignment: .leading) {
Text("2222222")
.background(Color.gray)
Text("333333333")
.background(Color.blue)
}
.alignmentGuide(.top, computeValue: { d in
DispatchQueue.main.async { // << dynamically detected - needs to be async !!
self.height = max(d.height, self.height)
}
return d[.top]
})
}
.background(Color.yellow)
}
}
Note: real result is visible only in LivePreview, because height is calculated dynamically and assign in next rendering cycle to avoid conflicts on @State.
Upvotes: 13
Reputation: 180
use .frame(maxHeight: .infinity)
var body: some View {
HStack(alignment: .top) {
Text("111")
.frame(maxHeight: .infinity)
.background(Color.red)
VStack(alignment: .leading) {
Text("2222222")
.frame(maxHeight: .infinity)
.background(Color.gray)
Text("333333333")
.frame(maxHeight: .infinity)
.background(Color.blue)
}
}.background(Color.yellow)
.frame(height: 50)
}
result: demo
Upvotes: 0