1

I have anUICollectionView inside aUITableViewCell and I have some issues regarding to Auto layout.

That's the hierarchy of my views.


UITableViewCell

   |
   -------ContentView
                |
                -------MyView
                          |
                          |-----UICollectionViewCell

Until here, all views are pinned up in his superviews., e.g: trailing space=0, leading space=0, top space=0, bottom space 0.

The height of myUICollectionViewCell is dynamic, based on the number of itens, which I draw usingUICollectionViewLayout. So it has a height constraint which is >= 100.

After the Items are in place, I call one delegate to update that height constraint, so myUITableView is able to calculate the right height for theUITableViewCell.

Even though this are working fine, sometimes I get the auto layout error above.

How can I avoid this?

enter image description here

Rodrigo Morbach
  • 378
  • 4
  • 16
  • By reading the console, it seems that you have a UICollectionView(not UICollectionViewCell) inside the UITableViewCell. First of all, you should avoid Scrolling views nesting. And if everything works fine for you, and you want to get rid of the console warning, do as suggested by console, just remove the constraint with height>=100. – BangOperator May 19 '15 at 12:11
  • You are right, It is a UICollectionView. I know that this is not a good approach, but is what I've got for now. The problem is, if I remove that constraint, the UITableViewCell is not able to calculate the height of the cell properly (I'm not very good at auto layout, by the way) – Rodrigo Morbach May 19 '15 at 12:26
  • Try setting height >= 100 to say height >=10. If works, just remember is not safe at all. – BangOperator May 19 '15 at 12:54

1 Answers1

2

The cell's contentView has a fixed height (44). That's causing the occasional conflict between the storyboard, and Auto Layout's derived height.

Self-sizing is the best way to go. What you want to do is to get your "collection view" to determine its intrinsic size, so Auto Layout can automatically handle the variable cell height for you.

See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.

He uses labels but the principle is the same. The cell asks its contentView to lay itself out. The contentView asks the label to lay itself out. The label determines its height. Auto Layout determines the contentView's height, based on the label's constraints, and the cell determines its height, based on the contentView's height. The point is that the label doesn't have a height constraint, and your "collection View" would no longer need one either.

Once you adopt self-sizing, it eliminates a lot of code (and size constraints), as containers manage their size, and constraints simply handle spacing.

Community
  • 1
  • 1