I tried to modify CustomLayoutProject in order to create some kind of ZStack which resizes child views to size of the largest one and positions them one over another with some vertical offset. And I'm stuck. Setup is next:
I've created EqualSizeZStack: Layout and put in instead of ButtonStack() in ContentView like this:
EqualSizeZStack {
Buttons()
}.border(.blue)
I've restricted buttons width by setting their text width .frame(width: 200)
instead of .frame(maxWidth: .infinity).
Updated Goldfish model text to be 4x longer, so related button can grow up in restricted width (and it does).
Layout container gets requested height, and largest size is calculated and assigned correctly (this logic was not changed).
But smaller buttons just refuse to apply largest button size no matter what I do. What did I miss?
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
guard !subviews.isEmpty else { return }
let maxSize = maxSize(subviews: subviews)
let placementProposal = ProposedViewSize(width: maxSize.width, height: maxSize.height)
var nextY = bounds.minY
for index in subviews.indices {
subviews[index].place(
at: CGPoint(x: bounds.midX, y: nextY),
anchor: .top,
proposal: placementProposal)
nextY += vOffset
}
}