Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I have multiple widths and heights of subviews in a UIStackView

Say for example I want to add 3 buttons to a stack view.

Button1 is 200 * 200 (1)

Button2 is 150 * 150 (0.75)

Button3 is 100 * 100 (0.5)

How can I accomplish this in interface builder?

like image 693
dubbeat Avatar asked Dec 03 '15 10:12

dubbeat


People also ask

What is a stack view?

Stack views let you leverage the power of Auto Layout, creating user interfaces that can dynamically adapt to the device's orientation, screen size, and any changes in the available space. The stack view manages the layout of all the views in its arrangedSubviews property.


1 Answers

You can do it in the storyboard using stackview

  1. Add a stackview with Alignment Center and Distribution Fill.
  2. Constraints on stackview is center horizontally and center vertically.
  3. Now add three buttons.
  4. Constraints on button1 is height and width = 200
  5. Constraints on button2 is height and width = 150
  6. Constraints on button3 is height and width = 100

The set up is shown in fig:

enter image description here

You are having conflicts because of Alignment Property which in your case it fill. make is center and its done.

Also we can add spacing between buttons. If we set Distribution Equal Spacing and add height constraint to stackview with height more than height of buttons.

This is vertical stackview. You can test the same with horizontal stackview.

Hope it will work for you.

like image 104
Irfan Avatar answered Sep 30 '22 02:09

Irfan