Home > database >  How to make one stack view compression priority less than other
How to make one stack view compression priority less than other

Time:12-28

I'm trying to make one stack view not collapse the labels in it, but setting the stack view compression and hugging priority is not giving any results.

image

image

The stacks are placed in a collection view cell. In the title stack there are 3 stacks with fill distribution and in the info stack there are 3 stacks with the same distribution.

I've pinned the title stack on top, bottom, and leading to superview, and trailing to the leading of info stack. And pinned info stack top, bottom, and trailing to superview.

I tried to set content & hugging priority that way that info stack won't collapse, as follows

[infoStack setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [infoStack setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    
[titleStack setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [infoStack setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

CodePudding user response:

It looks like you are trying to prevent the labels in the "info" stack view from collapsing by setting their content compression resistance and hugging priorities.

To achieve this, you should set the content compression resistance priority of the labels in the "info" stack view to a high value (such as UILayoutPriorityRequired), and set the content hugging priority of the labels to a low value (such as UILayoutPriorityDefaultLow).

For example:

infoLabel1.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .horizontal)
infoLabel1.setContentHuggingPriority(UILayoutPriorityDefaultLow, for: .horizontal)

infoLabel2.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .horizontal)
infoLabel2.setContentHuggingPriority(UILayoutPriorityDefaultLow, for: .horizontal)

infoLabel3.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .horizontal)
infoLabel3.setContentHuggingPriority(UILayoutPriorityDefaultLow, for: .horizontal)

You should also set the content hugging priority of the "title" stack view to a high value (such as UILayoutPriorityRequired), and set the content compression resistance priority of the "title" stack view to a low value (such as UILayoutPriorityDefaultLow).

For example:

titleStack.setContentHuggingPriority(UILayoutPriorityRequired, for: .horizontal)
titleStack.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, for: .horizontal)

This should prevent the labels in the "info" stack view from collapsing, and allow the "title" stack view to take up as much space as needed.

  • Related