Crystal
Crystal

Reputation: 29448

UIStackView embedded within UIStackView alignment

I'm trying to use embedded UIStackViews to get something that looks like this:

Label1 ---------<spacing>-------Label2
Label3 -<can take up this whole area>-

So I have label1 and label2 in one horizontal UIStackView. And then that innerStackView vertically stacked with Label3. But I'd like to have Label1 left aligned all the way to the left, and then label2 all the way on the right. I can add spacing, but it seems like there should be a better way to do this.

The reason why I'm trying to do it this way is because sometimes label2 and label3 are not populated and I read that UIStackViews can handle that layout really easily when one of those items are hidden and I wanted to try to learn something different than doing it with labels and AutoLayout.

Upvotes: 0

Views: 971

Answers (2)

Airel
Airel

Reputation: 23

You have this:
label1 and label2 stacked inside one horizontal stackView that we'll name S1. Then you stacked S1 together with label3 into a vertical stack that we'll referto by S2. OK??

My proposal:
Try to stack label1 into a vertical stackView (with alignment leading) S3, and label2 into another vertical stackView (with alignment trailing) S4. and then stack these two stackviews S3 y S4 inside S1. I think this should work fine.

Upvotes: 0

Josh Homann
Josh Homann

Reputation: 16327

The stackview alignment and distribution properties affect all arranged subviews the same way, ie you cannot have one left and one right. However, you can set the horizontal stackview to fill equally and set label1 text alignment to left and label2 text alignment to right and it will achieve the same effect.

Upvotes: 3

Related Questions