2

How can I layout a label and a button inside a UIStackView in such a way that the button only takes as much space as it needs and the rest is left for the label? Button text could be various length due to localizations.

The UIStackView here is alignment: center with distribution: fill.The only missing thing here is not stretching the button beyond the width needed for text and insets.

enter image description here

It doesn't seem possible with just the distribution settings. If Fill Proportionally is set the button must have a width constrain . Tried playing with the compression and hugging to no success, maybe I am doing something wrong.

EDIT: Found out why content hugging wasn't working. It needed a value >= 761 and not higher than 250 as I thought. Because 250 is the label content hugging value. Why 761? Can't really say.

h3dkandi
  • 1,106
  • 1
  • 12
  • 27

1 Answers1

8

You need to set the Content Hugging priority of your button to something higher than 250. That way the button does not want to grow bigger than it needs to.

mangerlahn
  • 4,746
  • 2
  • 26
  • 50
  • I did try it. Because you have provided evidence that it should work I made a new project and tried it there. It does work. I don't know what is wrong with my live project. Must investigate :/. Thank you for the response. – h3dkandi May 22 '17 at 10:56
  • Show me your stack view configuration, maybe I can help – mangerlahn May 22 '17 at 11:03
  • 1
    I found what was wrong. I needed >=761 content hugging on the button. Dunno why. When I tested I tested with 750 thinking that would be enough and then just gave up when it didn't work. – h3dkandi May 22 '17 at 11:10