0

I have a container group (the radio group) that contains a UIStackView, and that stack view contains a bunch of items that can have varied sizes, depending on their label content. They have two labels that could span multiple lines.

Unfortunately, I cannot get the content of the items to lay out correctly. I've tried reordering the constraints in many different ways, I've tried nesting the label content into a container view, but I can't seem to get the content of the stack view to layout out correctly. It will always either underflow and truncate the very end of the label, or it'll size just slightly too small and force the content to squeeze itself into the frame:

Incorrect layout for UIView frames in UIStackView

The constraints for the item are:

NSLayoutConstraint.activate([
   radioView.topAnchor.constraint(equalTo: topAnchor, constant: contentInsets.top)
   radioView.leftAnchor.constraint(equalTo: leftAnchor, constant: contentInsets.left),
   bottomAnchor.constraint(greaterThanOrEqualTo: radioView.bottomAnchor, constant: contentInsets.bottom),
   textLabel.topAnchor.constraint(equalTo: topAnchor, constant: contentInsets.top),
   textLabel.leftAnchor.constraint(equalTo: radioView.rightAnchor, constant: labelsSpacingLeft),
   textLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -contentInsets.right),
   detailTextLabel.topAnchor.constraint(equalTo: textLabel.bottomAnchor, constant: detailTextLabelSpacingTop),
   detailTextLabel.leftAnchor.constraint(equalTo: radioView.rightAnchor, constant: labelsSpacingLeft),
   detailTextLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -contentInsets.right),
   bottomAnchor.constraint(greaterThanOrEqualTo: detailTextLabel.bottomAnchor, constant: contentInsets.bottom),
])
lufinkey
  • 342
  • 4
  • 15

1 Answers1

0

Got this to work correctly using this answer. Just apply these properties to the labels:

label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
label.setContentHuggingPriority(.defaultHigh, for: .vertical)
lufinkey
  • 342
  • 4
  • 15