1

I have a grid made of iOS buttons (think Sudoku). I assigned each button to have aspect ratio 1:1. To further ensure their equality, I have made each button have width = height constraint set.

However, when I look at the buttons, some of them have 44x45, while others have 44x44 (as expected).

Is there a constraint that I'm missing or is there a workaround that can guarantee my buttons are perfect squares.

3
  • The fact that you have two constraints doing the same might be causing the problem. Try only using the ratio constraint. Commented Jan 6, 2016 at 3:25
  • Is there a quick way to remove a bunch of constraints/add them at a single go? Commented Jan 6, 2016 at 3:30
  • Ok anyways... that didn't end up solving the problem... Commented Jan 6, 2016 at 4:01

1 Answer 1

1

This is probably happening because you have other constraints affecting the views too. For eg. You might have put these in a container view and specified container views width or made container view's width equal to the width of screen.

Now when these square view fill the container view, then there is some extra space left say for eg like 7 points. What autolayout does is that it adds these 7 points making some of your squares wider by 1 point.

This can be fixed by providing the correct constraints for the container view.

Sign up to request clarification or add additional context in comments.

3 Comments

This might be the cause... but how do I distribute it in a way that my boxes stay square (like I dont care if all the squares are 44x44 instead of some being 44x45...) Also none of the squares are 45x45, which makes me confused as to why autolayout would distribute those points to the squares instead of just letting the extra space exist...
Set the container view's leading margin to the first box and the trailing margin to the last box. And remove any other constraint that could affect the width of the container view.
Im using labels as the only things constraining the width of the boxes (and to center them with a border around my boxes). So basically I have two labels set on both sides. I set the two labels to have equal width and their trailing/leading space to = 0 to the first/last boxes respectively. I have the labels' leading/trailing space to the edge of the view controller I'm in. I don't believe I have any other constraint on widths other than the fact the boxes have to have equal width as height.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.