0

I have a collectionview where I'm trying to do 2 cells on each row. It looks like it's building the cells from my prototype cell and centering everything properly. Then it runs my sizeForItemAtIndexPath and makes the sizing correct (2 cells per row), but then all the labels are off center as it was laid it before.

I have been screwing with invalidateLayout and viewDidLayoutSubviews but nothing seems to be working. Any ideas how to force the cell to re-layout the labels after it has run sizeForItemAtIndexPath? Thanks in advance.

EDIT: Here are two images. This is the first image sizeForItemAtIndexPath is commented out, everything in the cell is formatted perfectly, but the spacing is off: enter image description here Here is the second, with the sizeForItemAtIndexPath making the collectionView have proper spacing. Here is where the alignment is messed up within the cell, not centered. enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
tahoecoop
  • 378
  • 2
  • 9
  • 30
  • Could you just use constraints to lay out the subviews in your cells? – rob mayoff Oct 21 '15 at 18:44
  • I have it laid out in the prototype cell of the collectionview, everything is constrained to the center. It just seems to build the cell, and then size it, so everything is off center. – tahoecoop Oct 21 '15 at 18:47
  • some screenshots would really help. I might have a solution to this, if I know exactly what you are trying to do. Also check this to see if it helps http://stackoverflow.com/questions/32472956/ios-uicollectionview-dynamically-change-cells-height/33266661#33266661 – ngoa Oct 21 '15 at 18:53
  • I added 2 photos on what is happening. Thanks! – tahoecoop Oct 21 '15 at 19:04

1 Answers1

1

I have had the experience of the contentView not resizing correctly in my cells when they are changed to a different size than is present in the storyboard with collectionView(_:layout:sizeForItemAtIndexPath:). Try adding the following after you dequeue your cell:

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

If you're using a custom subclass of UICollectionViewCell you can put that code in an override of awakeFromNib():

override func awakeFromNib() {
    super.awakeFromNib()

    contentView.frame = cell.bounds
    contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
}
Charles A.
  • 10,685
  • 1
  • 42
  • 39