Search code examples
iosswiftautolayoutuistackview

Align two elements by content in StackView


I have two UIView in StackView Horizontal. In UIView posted UILabel. I need the first one to be the priority. The main requirement is a complete lack of a fixed width in any UI. enter image description here


Solution

  • Set the setContentCompressionResistancePriority(_:for:) higher for the required label and lower for the other one. Example:

    textLabel1.setContentCompressionResistancePriority(UILayoutPriorityDefaultHigh, forAxis: .Horizontal)
    textLabel2.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: .Horizontal)