24

Since iOS 8 [UIColletionViewCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] returns a size with height of 0.

Here's what the code does:

To determine the size for cell in a UICollectionView in iOS 7 I use systemLayoutSizeFittingSize: on a cell defined in a xib file using auto layout. The size depends on the font size of an UILabel being a subview of the UICollectionViewCell in my xib file. The label's font is set to UIFontTextStyleBody. So basically the cell's size depends on the font size setting made in iOS 7.

Here is the code itself:

+ (CGSize)cellSize {
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([MyCollectionViewCell class]) bundle:nil];

    // Assumption: The XIB file only contains a single root UIView.
    UIView *rootView = [[nib instantiateWithOwner:nil options:nil] lastObject];

    if ([rootView isKindOfClass:[MyCollectionViewCell class]]) {
        MyCollectionViewCell *sampleCell = (MyCollectionViewCell*)rootView;
        sampleCell.label.text = @"foo"; // sample text without bar

        [sampleCell setNeedsLayout];
        [sampleCell layoutIfNeeded];

        return [sampleCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    }

    return CGSizeZero;

}

It works perfectly fine in iOS 7 but not in iOS 8. Unfortunately I have no clue why.

How can I get the auto layout size of the UICollectionViewCells in iOS 8?

PS: Using

 return [sampleCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

instead of

 return [sampleCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

as somebody might suggest, doesn't make any difference.

12
  • I have the same issue: on iOS 7 [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height returns the right size: on iOS 8 always 0. I find this is building with either iOS7 OR iOS 8 SDKs. Sorry I can't help yet, but happy that I'm not the only one! I've filed this radar: 17959753 Aug 12, 2014 at 16:40
  • I have exactly the same problem. I have been playing around with it the whole day and couldn't find a solution. Changing it to collectionViewCell.contentView did not help.
    – davidk
    Aug 13, 2014 at 6:26
  • I am having the same problem, experimenting has shown that it might have something to do with the constraints being added via interface builder which are against the cell view instead of the cell.contentView. Adding constraints with code against the contentView solves this issue partially
    – user1488696
    Aug 13, 2014 at 8:34
  • @AlexLittlejohn it's UICollectionViewCells IB hides the content view, whereas with UITableViewCells it doesn't, which makes it harder to know where the constraints are being created. Like maremmle said in the question, [sampleCell.contentView systemLayoutSizeFittingSize:...] works in table view cells. The weird thing here is that it's definitely broken/changed on iOS 8, since building with either SDK fails on an iOS 8 device. Aug 13, 2014 at 8:57
  • @AlexLittlejohn what do you mean by solving partially? Does the height come back as non-zero? Aug 13, 2014 at 8:58

8 Answers 8

8

What you need to do is wrap all of your content in a container view, then call:

return [sampleCell.containerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

Your cell should look like this: cell -> containerView -> sub views

This works on both ios7 & ios8.

Supposedly on ios8, all you have to do is set the estimateSize & cell will automatically auto size on its own. Apparently that's not working as of beta 6.

1
  • Strange as it may seem, the container view totally works: I guess they broke something in UICollectionViewCell :S Sep 10, 2014 at 11:49
7

Looks like this officially a bug: I filed a report that was closed as a duplicate of this one

Will report back when Beta 6 is out.

[Update: working properly in the GM seed of iOS 8, and the bug has been closed by Apple.]

11
  • Thanks for your effort. Should i file a bug as well or do you think it will be fixed in Beta6 anyway?
    – martn_st
    Aug 14, 2014 at 9:52
  • I think it's firmly on the radar now: maybe file one if it's not fixed in 6? Aug 15, 2014 at 10:24
  • @maremmle You should always file a bug, even if it's a duplicate. Duplicate bug reports will raise the visibility of a particular bug to Apple, making it more likely to be addressed sooner rather than later.
    – memmons
    Aug 30, 2014 at 19:52
  • 1
    @MichaelG.Emmons Alright I also submitted a bug report to Apple.
    – martn_st
    Sep 2, 2014 at 14:10
  • 1
    My bug report was also marked as a duplicate of 17962954. openradar.appspot.com/search?query=17962954 Sep 5, 2014 at 7:26
4

We're using a work-around for now, copied below. Hopefully, these issues will be resolved before iOS 8 release and we can remove this. (The kludge assumes knowledge of Apple's implicit contentView behavior, and we have to hack IB outlet references to any constraints we transfer.)

We notice they're also removing all autoresizingMasks from storyboards/NIBs during upgrade, which makes sense given it's supposed to be auto-layout, but collection views still throwback to springs & struts. Perhaps this has been overlooked in the purge?

--Dan

/**
 Kludge around cell sizing issues for iOS 8 and deployment to iOS 7 when compiled for 8.  Call this on the collection view cell before it is used, such as in awakeFromNib.  Because this manipulates top-level constraints, any references to such initial constraints, such as from IB outlets, will be invalidated.

 Issue 1: As of iOS 8 Beta 5, systemLayoutSizeFittingSize returns height 0 for a UICollectionViewCell.  In IB, cells have an implicit contentView, below which views placed in IB as subviews of the cell are actually placed.  However, constraints set between these subviews and its superview are placed on the cell, rather than the contentView (which is their actual superview).  This should be OK, as a constraint among items may be placed on any common ancestor of those items, but this is not playing nice with systemLayoutSizeFittingSize.  Transferring those constraints to be on the contentView seems to fix the issue.

 Issue 2: In iOS 7, prior to compiling against iOS 8, the resizing mask of the content view was being set by iOS to width+height.  When running on iOS 7 compiled against iOS 8 Beta 5, the resizing mask is None, resulting in constraints effecting springs for the right/bottom margins.  Though this starts out the contentView the same size as the cell, changing the cell size, as we do in the revealing list, is not tracked by changing it's content view.  Restore the previous behavior.

 Moving to dynamic cell sizing in iOS 8 may circumvent this issue, but that remedy isn't available in iOS 7.
*/
+ (void)kludgeAroundIOS8CollectionViewCellSizingIssues:(UICollectionViewCell *)cell {

    // transfer constraints involving descendants on cell to contentView
    UIView *contentView = cell.contentView;
    NSArray *cellConstraints = [cell constraints];
    for (NSLayoutConstraint *cellConstraint in cellConstraints) {
        if (cellConstraint.firstItem == cell && cellConstraint.secondItem) {
            NSLayoutConstraint *parallelConstraint = [NSLayoutConstraint constraintWithItem:contentView attribute:cellConstraint.firstAttribute relatedBy:cellConstraint.relation toItem:cellConstraint.secondItem attribute:cellConstraint.secondAttribute multiplier:cellConstraint.multiplier constant:cellConstraint.constant];
            parallelConstraint.priority = cellConstraint.priority;
            [cell removeConstraint:cellConstraint];
            [contentView addConstraint:parallelConstraint];
        } else if (cellConstraint.secondItem == cell && cellConstraint.firstItem) {
            NSLayoutConstraint *parallelConstraint = [NSLayoutConstraint constraintWithItem:cellConstraint.firstItem attribute:cellConstraint.firstAttribute relatedBy:cellConstraint.relation toItem:contentView attribute:cellConstraint.secondAttribute multiplier:cellConstraint.multiplier constant:cellConstraint.constant];
            parallelConstraint.priority = cellConstraint.priority;
            [cell removeConstraint:cellConstraint];
            [contentView addConstraint:parallelConstraint];
        }
    }

    // restore auto-resizing mask to iOS 7 behavior
    contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
}
1
  • is this perhaps also applicable to iOS7?
    – QED
    Sep 10, 2014 at 19:00
0

This is a bug in xCode6 and iOS 8 SDK running on iOS7 devices :) It almost waisted my day. Finally it worked with the below code in the UICollectionViewCell sub class. I hope this will be fixed with the next version

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    self.contentView.frame = bounds;
}
1
  • I do not recommend this fix. If you use systemLayoutSizeFittingSize: it will not work correctly since the contentView won't be part of the constraint hierarchy. You need to constraints on the contentView or set its autoResizingMask.
    – phatmann
    Oct 3, 2014 at 17:45
0

I had the same issue for UITableViewCells and iOS 7 (ios8 works perfectly), but "Triet Luong" solution worked for me:

return [sampleCell.containerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
0

This is not a bug, I have been grappling with this issue now for some time and have tried different things, I am giving below the steps that's worked for me:

1) use contentView [sampleCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

2) you can create your own contentView and have the subViews added to the contentView, don't forget to pin the top, bottom and left and right of the contentView if you are using a custom contentView to the superView, if you don't do this then height will be 0, here also depending upon your requirements you can for. e.g not pin the bottom of the contentView to the superView so that the height of the view can vary, but pinning is important and pinning which one depends on your requirements.

3) set the constraints properly in interface builder depending upon your requirements, there are certain constraints that cannot be added in interface builder, but then you can add them in viewDidLoad of the viewController.

So my 2 cents input is that constraints have to be set properly in the interface builder for systemLayoutSizeFittingSize:UILayoutFittingCompressedSize to return correct dimensions, this is the solution guys.

0

maybe you have some wrong constrain, you should specify both virtical top space and bottom space between your UIView and contentView, i also had this issue before, that is because i just specified the virtical top space, and didn't specfied the virtical bottom space to contentView

-1

It was a bug in iOS 8 beta versions. Finally it is fixed with for iOS 8 GB (Build 12A365). So for me now it works with the same code I wrote for iOS 7. (see the question)

1
  • Thx to @all involved in the discussion and reporting bugs to Apple.
    – martn_st
    Sep 12, 2014 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.