Shift72
Shift72

Reputation: 448

How to set constraints to multiple UIImageView's in Swift

After hours of attempting to find a solution, as well as trying and failing myself I decided to ask here.

As shown on the picture below I have 15 Image views in a pyramid shape that I would like to set constraints on.

enter image description here

I have tried to:

  1. Constraint equal width and height on all images
  2. Constraint center image on line 1, 3 and 5 to horizontal center
  3. Align vertical centers to all images on each line
  4. Etc etc

The result is always the image view going out of bounds or similar issues. (Clip to Bounds is true)

Anyone have a solution or suggestions?

Upvotes: 1

Views: 288

Answers (2)

E. Huckabee
E. Huckabee

Reputation: 1808

try putting them all in stack views then putting all those stack views inside a stack view and putting constraints on that

like this take a look at the view hierarchy on the left

image 1

to get the spacing i achieved just adjust the spacing on the right inside the attributes inspector

and its even able to have constraints on the highest stack view

image 2

all the errors go away once you add constraints to the top stack view

like shown here

image 3

Upvotes: 2

Damien
Damien

Reputation: 3362

Embed all your imageViews in a UIView.

set the top, bottom, leading and trailing constraint inside that view (top image to the superview, etc.).

align the UIView horizontally and vertically in the container.

You probably also want to set a width to your UIView, but I let you think about the final result you want.

Upvotes: 0

Related Questions