Dave Abrahams explained some of the mechanics of SwiftUI layouts in his WWDC19 talk about Custom Views, but he left out some bits and I have trouble getting my views properly sized.
Is there a way for a View to tell its container that it is not making any space demands, but it will use all the space it is given? Another way to say it is that the container should hug its subviews.
Concrete example, I want something like c:
If you have some Text
s inside a VStack
like in a), the VStack
will adopt it's width to the widest subview.
If you add a Rectangle
though as in b), it will expand as much as it can, until the VStack
fills its container.
This indicates that Text
s and Rectangle
s are in different categories when it comes to layout, Text
has a fixed size and a Rectangle
is greedy. But how can I communicate this to my container if I'm making my own View
?
The result I actually want to achieve is c). VStack should ignore Rectangle (or my custom view) when it determines its size, and then once it has done that, then it should tell Rectangle, or my custom view, how much space it can have.
Given that SwiftUI seems to layout bottom-up, maybe this is impossible, but it seems that there should be some
way to achieve this.