UIStackView with proportional width to superview

uistackview fill proportionally programmatically
uistackview subview width
uistackview width based on content
uistackview dynamic height
uistackview programmatically
uistackview content hugging
uistackview divider
uistackview alignment

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

UIStackView Tricks: Proportional Custom UIViews with 'Fill , How to use the Fill Proportionally option with any UIStackView custom simply alter the width of the CGSize returned by intrinsicContentSize . UIStackView ensures that its arranged subviews maintain the same proportion to each another as your layout grows and shrinks. However, unlike the other distribution options, views that are proportional must have an intrinsic content size. The trouble with that is not all views have an intrinsic size, including UIView itself. Happily, there is a workaround that allows us to adjust proportions of arbitrary UIViews.

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.

Proportional Spacing with Auto Layout, Proportional Spacing with Auto Layout. Jan 24, 2016 · 5 minute read It has two rows of image views that are all positioned based on some percentage of the super view width and height. A center constraint with a modifier (e.g stackView. Instead of Fill, change the Distribution of the UIStackView to Fill Proportionally and add constraints for the width of each element. Those constraints will serve as a guide to proportions on one element to each other and not as fixed sizes.

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

Auto Layout Guide: Stack Views, Horizontally, each view is stretched to match the stack view's width. For more information on pinning a view to fill its superview, see Attributes and Adaptive Single View. @IBOutlet weak private var stackView: UIStackView! I have a UIStackView where the middle button is only visible in a different size class storyboard view After rotating the device the button will be visible because of its size class and is also in the right view hierarchy (image), but is has not enough constraints given by UIstackview to be positioned correctly, it is positioned on the upper

Auto Layout Guide: Views with Intrinsic Content Size, In this example, the label's width is based on the size of its text property, The text fields expand to fill all the remaining space in the superview. Set the width of each view equal to the parent stack view multiplied by its proportion of the total intrinsic content width. I've attached a rough example of a horizontal proportional stack view below, you can run it in a Swift Playground.

iOS, You can use multiplier to create proportional layout for different size factor. Example: Turquoise View (V1) is a square with width proportional superview width  Appetizer 🥗 UIStackView Essentials Introduced in iOS 9, UIStack View is the most recent addition to the UI control assortment in Cocoa Touch. On the surface, it looks similar to its older AppKit sibling, the NSStack View, but upon closer inspection, the differences between the two become clearer.

UIStackView: A Field Guide, And its assigned distribution (fill, fill equally, or proportionally, center ahhh let stackView = UIStackView() //Horizontal axis by default//Widths will be I use to set top/bottom/leading/trailing constraints to another superview UIStackView has simplified such tasks. You can easily lay out a series of views horizontally or vertically within a stack view and set it up in a way that the views are automatically adjusted to the available space using properties such as alignment, distribution and spacing.

Comments
  • Likely a duplicate of stackoverflow.com/questions/26373598/…
  • It makes superview width smaller. Superview width is reset to 0.3 of original width.
  • Then you’re not doing what I said. You are not using the constraint multiplier, or not using it correctly.