52

I have a simple UICollectionView with cells that have a single UITextView. The UITextView is constrained to the edges of the cell, so they should stay the same size as the cell size.

The problem I'm having is that for some reason these constraints are not working when I specify the cell size via collectionView:layout:sizeForItemAtIndexPath:.

I have the cell size set to 320x50 in the Storyboard. If I return a size with 2 times the height of the cell size with sizeForItemAtIndexPath:, the UITextView stays the same height despite the constraints I set. I am using Xcode 6 GM.

My view controller code is:

@implementation TestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UICollectionViewCell *c = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];

    NSLog(@"%f", c.frame.size.height);

    UITextView *tv = (UITextView *)[c viewWithTag:9];
    NSLog(@"%f", tv.frame.size.height);

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;

    CGSize size = flowLayout.itemSize;

    size.height = size.height * 2;

    return size;
}

@end

Those logs in viewDidAppear: give me an output of:
100.00000
50.00000
As you can see, the UITextView height doesn't change with the cell height.


Here's a screenshot of the storyboard I set up with a UITextView constrained in the UICollectionViewCell:

enter image description here

I know using auto layout constraints works fine with UITableViewCells and dynamic resizing. I have no idea why it's not working in this case. Does anyone have any ideas?

ryanthon
  • 2,494
  • 3
  • 14
  • 18

4 Answers4

111

Well, I just looked on the iOS developer forums. Apparently, this is a bug with the iOS 8 SDK running on iOS 7 devices. The workaround is to add the following to your subclass of UICollectionViewCell:

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    self.contentView.frame = bounds;
}
override var bounds: CGRect {
    didSet {
      contentView.frame = bounds
    }
}
Iulian Onofrei
  • 7,489
  • 8
  • 59
  • 96
ryanthon
  • 2,494
  • 3
  • 14
  • 18
  • 1
    Great ! you save my day ! this solution working for me after installing iOS 8 xcode 6 etc. all my tableview didn't adapt the uicollectionview cell size. Thank you :) – Fjohn Sep 25 '14 at 09:38
  • 1
    in case it didn't work for you try this: awakerFromNib(){ contentView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight } – Alexander Polovinka Nov 07 '14 at 21:27
  • I had same issue on iPad with iOS 8, and this solution fixed the problem as well. Thanks! – almas May 04 '15 at 19:37
  • 2
    +1 on this being an issue in iOS 8 too. I had a collection view cell that was working fine in iOS 7, but not in iOS 8. This fixed it. – ChrisH May 07 '15 at 21:08
  • Totally forgot about this. Thanks...still an issue with latest iOS 8 SDK running on iOS 7 devices. – Ari Braginsky Jun 10 '15 at 11:32
  • I have faced with the same issue on iOS 9, Does it still not fixed? – CAMOBAP Dec 29 '15 at 15:51
42

Equivalent Swift Code :

override var bounds: CGRect {
    didSet {
      contentView.frame = bounds
    }
}
HamzaGhazouani
  • 5,674
  • 5
  • 28
  • 38
25

Here is the solution, if you are not subclassing UICollectionViewCell. Just add this two lines under your cellForItemAtIndexPath: after dequeueReusableCellWithReuseIdentifier:

Obj-C

[[cell contentView] setFrame:[cell bounds]];
[[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

Swift - 2.0

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
itsji10dra
  • 4,550
  • 3
  • 36
  • 55
4

Swift 2.0:

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
Bill Chan
  • 2,416
  • 27
  • 26