Search code examples
iosuicollectionviewuicollectionviewcellnslayoutconstraint

Constrain problem involving UICollectionViewCell and subviews defies logic


I have a collectionviewcell with a button and a label like this:

enter image description here

The button is a 100x100 square, the label is a 100x40 rectangle. The cell is adjusted to 120x140.

So the constraints I have added are:

Button:

  • aspect ratio 1:1
  • top to superview space 0
  • bottom to label space 0
  • center horizontally in respect with cell
  • width = 100

LABEL

  • same width as button
  • center horizontally in respect to button
  • height 40
  • top space to button 0
  • bottom space to cell 0

I see constrain errors everywhere.

I have tried to embed these two views on a stack. It craps everything.

Then I have tried to embed the stack into a view, same problem.

This defies any logic.

Can you guys tell me how in the name of heaven I constrain these elements? All I want is both elements centered horizontally, the button on top, the label on the bottom, both with the same width of 100, the button squared and the label with a hight of 40. Both in a cell of 120x140.

Thanks


Solution

  • This is how you should set your constraints using a stack view in a cell to accomplish what you need: (cell width = 120, height = 140)

    enter image description here