ContentHuggingPriority and CompressionResistance not honored by StackView with custom control as subview

52 views Asked by At

I've created a horizontal UIStackView with two subviews:

  1. A UILabel.
  2. A custom UIControl.

I would like the Label to expand within the view and for the Control to hug its content.

Content Hugging Priority is set to (in Interface Builder):

  • Label: 240, Control: 255

Content Compression Resistance Priority is set to (in Interface Builder):

  • Label: 756, Control: 732

As you can see in the screenshot below, the label is being hugged by its bounds and the control is expanding to fill the remaining space, which is the opposite of what I've specified in the size inspector.

What am I missing here?

Screenshot of StackView

Other Notes:

  • I don't have any other constraints set for Label or Control.
  • The StackView's spacing is set to 0 and it has leading and trailing constraints set to its superview.
  • If I remove the custom control and just add another label to the StackView, the hugging and resistance properties work as expected.
1

There are 1 answers

5
SemperFly On

I was able to solve it by doing the following.

  1. The custom control contained a horizontal stack view with two subviews. I removed the stack view, replaced it with a view as the new parents of the subviews, and added constraints to achieve the look I was going for.
  2. Programmatically, within the initialization function of the parent view, I called customControl.sizeToFit() followed by setNeedsLayout() after calling setupNibView().