First off, do you really need a stack view for this? It would be much easier to arrange this simply using proportional height constraints directly.
However, you can do this with a stack view if you really want to use a stack view. The secret is that the "weight" in question is simply the arranged view's intrinsicContentSize().height
. Knowing this, I was easily able to set up a stack view consisting of three image views in the proportions you request:
Those, for purposes of the demonstration, are the same image repeated three times: one at 3x height, one at 6x height, and one at 1x height.
How did I do it? I gave the three image views tag
values of 300, 600, and 100 respectively in the storyboard. (Of course I could have used an IBInspectable custom property for this, and in real life, I would do so.) Then I made them all instances of my UIImageView subclass, MyImageView, whose code looks like this:
class MyImageView: UIImageView {
override func intrinsicContentSize() -> CGSize {
print(self.tag)
return CGSizeMake(CGFloat(self.tag), CGFloat(self.tag))
}
}
The stack view's Distribution is configured as Fill Proportionally. The image views are configured with their Content Mode as Scale To Fill. Result: The stack view, in laying out its arranged views, consults the intrinsicContentSize
method, and thus does the right thing.