Please advise on how should I approach implementing the following layout using Xcode's Auto Layout?
The idea here is that redView
and all blueViews
are all perfect squares with equal spacing.
I was only able to create working version with the redView
and 2 vertical blueView
without the bottom 3 blueViews
.
Here is how I would approach it.
1:1
. Add a constraint to center it horizontally in the view. Give it leading edge and top edge constraints.B
. Give it an aspect ratio of 1:1
. Align it with the top edge and trailing edge of the yellow square.C
. Align it to the trailing edge of yellow and center it vertically in yellow. Add equal width and equal constraints to view B
. Give it a vertical space of 16
from view B
.F
. Align it to the trailing edge and bottom edge of yellow. Give it equal width and height of view C
.E
. Align it to the bottom edge of yellow. Center it horizontally in yellow. Give it equal width and height to F
.D
. Align it to the leading edge and bottom edge of yellow. Give it equal width and height to E
.A
. Give it aspect 1:1
. Align it to leading edge of yellow and top edge of yellow. Align its bottom edge with bottom edge of C
.clear
.That will do it. The yellow square will resize for all devices, and the red and blue squares will be sized accordingly. You can change the distance constraint between views B
and C
and all gaps will adjust automatically, which makes this easy to adjust for the desired look.
Here is an example running in the simulator. I hooked up the slider to change the value of the constant
for the constraint setting the distance between views B
and C
.