Reputation: 171
I have a ViewController with a UIStackView aligned to its parent container with some margins (Leading, Trailing, Top Space, Bottom) and Vertical Axis. UIStackView is set to Alignment "Fill" and Distribution "Equal Spacing". Inside the UIStackView, I have three UIScrollViews as children. Each UIScrollView has a UIView with some controls inside.
If I set the UIStackView in IB to Distribution "Fill" and if I set the second UIScrollView's Content Hugging Priority to 200, and leave the first and third UIScrollViews to their default of 250, I see that the second UIScrollView is stretched properly to fill the empty space, as expected and desired.
However, I want to set the UIStackView to Distribution "Equal Spacing" by default in IB, and in code, when the user taps one of the UIScrollViews, I want the tapped UIScrollView to expand and fill up the rest of the space. So I added the following code on tapping a UIScrollView (in order to set that tapped UIScrollView's CHP to a lower value than its peers):
scrollViewToExpand.setContentHuggingPriority(UILayoutPriority(rawValue: 200), for: .vertical)
stackView.distribution = .fill
However, the first UIScrollView always gets stretched and the second and third UIScrollViews never get stretched.
Each descendant UIView of each UIScrollView is set to trailing/leading/bottom/top matching SuperView and equal widths.
What am I doing wrong?
Upvotes: 1
Views: 1582
Reputation: 171
I finally figured it out. Firstly, it seems like I had to set the CHP not on the UIScrollView, but rather on a a child that can expand / contract.
Furthermore, I had to change a constraint I set on each UIScrollView's embedded UIView that defined the height as >= 60, to be equals to 60 when collapsed, and back again when not collapsed.
Lastly, the expansion / contraction does not happen at all if no child elements are present that can expand / contract. Merely having a UIView inside a UIScrollView is not enough.
Upvotes: 1