1

I have a horizontal UICollectionView, and each cell holds a UIImageView. When I position the UIImageView inside the cell:

the storyboard looks like this(notice the UIImageView takes the entire space of the UICollectionViewCell with an identifier postInnerCell)enter image description here

I think I have set the constraint using autoLayout correctly, with each side of the UIImageView pined to the margins of the UICollectionViewCell. Like this:

enter image description here

When I ran the simulator, however, it looks like this:enter image description here

The image does not fill up the entire space of the collectionViewCell(with the gray background color). The the content mode of the UIImageView is set to be aspect fill in the interface builder.

Here is my view hierarchy: enter image description here

**Note that the entire UICollectionView is a cell of the tableView(Basically, what I try to accomplish is each row of the tableView is a horizontal slider). I'm not sure that's the problem. Anyone have any idea about why such a issue occurs. **

Kesong Xie
  • 1,316
  • 3
  • 15
  • 35

3 Answers3

3

It turns out that I need to re-layout the subview by calling cell.layoutIfNeeded() before I return the cell in the collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) method. Thanks to the contribution of answer here UICollectionViewCell content wrong size on first load

Community
  • 1
  • 1
Kesong Xie
  • 1,316
  • 3
  • 15
  • 35
0

Set Content hugging priority of the image view to Low (250).

Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
0

IIRC, the content view of the cell will resize without triggering AutoLayout, so you have to manually either trigger a layout pass (e.g. .needsLayout = true or add constraints from the content view to the cell

DeFrenZ
  • 2,172
  • 1
  • 20
  • 19
  • what do you mean by adding constraints from the content view to the cell, you mean the constraint between the contentView to the collectionview? – Kesong Xie Jun 02 '16 at 16:15
  • The view hierarchy you have is: Collection View > Cell(s) > Content View > Image View. I'm talking about constraints from the content view to the cell. Try the view debugger to see what I'm talking about. – DeFrenZ Jun 02 '16 at 16:16