Johan Nordberg
Johan Nordberg

Reputation: 3777

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

Upvotes: 6

Views: 5701

Answers (2)

Vivek Parihar
Vivek Parihar

Reputation: 2318

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]
}

Upvotes: 3

Johan Nordberg
Johan Nordberg

Reputation: 3777

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;
}

Upvotes: 11

Related Questions