Kesong Xie
Kesong Xie

Reputation: 1396

Why the UIImageView does not fill the entire CollectionViewCell even I have all the constraints

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. **

Upvotes: 1

Views: 1269

Answers (3)

Kesong Xie
Kesong Xie

Reputation: 1396

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

Upvotes: 3

DeFrenZ
DeFrenZ

Reputation: 2252

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

Upvotes: 0

Sonny Saluja
Sonny Saluja

Reputation: 7287

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

Upvotes: 0

Related Questions