Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraints not updated in UICollectionViewCell subclass on iOS7

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

like image 635
Johan Nordberg Avatar asked Sep 23 '14 19:09

Johan Nordberg


2 Answers

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;
}
like image 110
Johan Nordberg Avatar answered Sep 28 '22 02:09

Johan Nordberg


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]
}
like image 29
Vivek Parihar Avatar answered Sep 28 '22 00:09

Vivek Parihar