4

I have a collection view based layout with different cell sizes depending on the content. A regular cell is 200x200 px, but if there is no content I display a cell with the same size as the collection view itself.

I use

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath

to calculate the correct size.

How ever, my subviews to the cell does not update its constraints. The cell is really simple, just a UILabel that should be centered within the cell superview. I have a horizontal and a vertical center constraint (also tried to pin each edge to the superview). The result is that the subviews gets the same size and position as entered in Interface Builder (Storyboard).

I've set background colors for both the cell and the label and can see that the cell gets the correct size, but the label does not.

The problem only exists in iOS7 and works as it should in iOS8.

Please help!

screenshot

Johan Nordberg
  • 3,329
  • 3
  • 26
  • 52
  • This is an interesting question. By the way I recently experienced a similar issue but in the opposite way: the subview inside the collection view cell was aligned correctly to its constraints after the parent view resizing in iOS7 while this didn't happen in iOS8: in the latter case as a walkaround I used the -layoutSubiews method to fix the iOS8 issue. It is curious you're experiencing the same issue differently for the two iOS versions. I don't know the answer yet, no time to dig into it... I had to quickly release a iOS8 update! – viggio24 Sep 23 '14 at 19:16
  • I started my project as a iOS8 project in Xcode 6 and later lowered the base target version. Maybe this behavior depend on what version the project was created for. – Johan Nordberg Sep 23 '14 at 19:23
  • How did you fix it in -layoutSubviews in your project? – Johan Nordberg Sep 23 '14 at 19:24
  • I followed the suggestions from this excellent article: http://oleb.net/blog/2014/03/how-i-learned-to-stop-worrying-and-love-auto-layout/ What I did at the end of the layourSubviews method was to set the subview frame to the cell frame. – viggio24 Sep 23 '14 at 19:43

2 Answers2

11

Thank you Stackover flow related questions pane! Found this thread and it solved my problem.

Auto Layout in UICollectionViewCell not working

I put this in my UICollectionViewCell subclass:

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    self.contentView.frame = bounds;
}
Community
  • 1
  • 1
Johan Nordberg
  • 3,329
  • 3
  • 26
  • 52
  • This takes the label behind the cell. I calculate the cell's height by this newSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; – Suresh Varma Oct 06 '14 at 13:11
3

For me when this issues is coming on IOS9 with swift 2. I just called awakeFromNib() and set autoresizingMask in UICollectionViewCell.

My UICollectionViewCell looks like this-:

override func awakeFromNib() {
   self.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth , UIViewAutoresizing.FlexibleHeight]
}
Vivek Parihar
  • 2,268
  • 16
  • 18