0

I need to increase the size of horizontal UIStackView depending on the width of superview. All buttons in UIStackView are fixed width & height. I tried setting autolayout constraint with stackview.width = superview.width * 0.3 but it messes the superview width. What is the correct way to achieve it?

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
  • Likely a duplicate of https://stackoverflow.com/questions/26373598/how-to-create-percentage-of-total-width-using-autolayout – matt Dec 31 '18 at 20:52

3 Answers3

2

This

1-"buttons in UIStackView are fixed width & height"  

Contradicts

2- "stackview.width = superview.width * 0.3"

As you need to make something of them flexible

So either ignore static widths,heights for the buttons and set

stackView.distriburion = .fillEqually

to make them equally spreaded , Or ignore proportional width of the stackView and set

stackView.distriburion = .fill

to make it grow according to size

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

I need to increase the size of horizontal UIStackView depending on the width of superview. All buttons in UIStackView are fixed width & height. I tried setting autolayout constraint with stackview.width = superview.width * 0.3 but it messes the superview width

I don't know what you mean by "it messes the superview width". This is what the constraint multiplier is for. If you are using the multiplier correctly, you will get the desired result.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You have to play with the horizontal hugging priority and the horizontal compression resistance priority.

By decreasing the compression and hugging priority of you stackView you tell to the autoLayout to size the stackView to fit the superView and not the superView to fit the stackView.

Hope it helps