简体   繁体   中英

UIStackView with proportional width to superview

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?

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM