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?
-
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 Answers
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

- 98,760
- 8
- 65
- 87
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.

- 515,959
- 87
- 875
- 1,141
-
It makes superview width smaller. Superview width is reset to 0.3 of original width. – Deepak Sharma Jan 01 '19 at 07:45
-
1Then you’re not doing what I said. You are not using the constraint multiplier, or not using it correctly. – matt Jan 01 '19 at 08:27
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

- 146
- 2