issue with collection view sizing

Noticed a bunch of my collection views in beta 3 are showing up too tall, but immediately shrink to size when I scroll. Looks like it is setting the cell size to layout.estimatedItemSize and not resizing to fit prior to drawing (these are autosized, with full constraints).


I've solved for now by making my view controllers UICollectionViewDelegateFlowLayout, and doing:


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

... create dummy cell

... populate cell with appropriate label text, etc., to make it the right size

let size = cell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)

return size

}


I think this is a bug in beta 3 since it worked fine in the first two betas and 11.x, but could be my constraints are weird or something.

Replies

Seeing this too in beta 4. Works fine in 11.x.


* layout.estimatedItemSize is UICollectionViewFlowLayoutAutomaticSize.


* Constraints completely specify the cell.


The initial size on screen is the default (50x50). Scrolling/rotating/resizing causes the layout to update. Invalidating the flow layout does not.


Off to see if I can create a simple reproducible case.

Post not yet marked as solved Up vote reply of sjs Down vote reply of sjs

Has anyone heard from Apple on this issue? I am having the same issue on my project.

Having the same issue. Checked on latest Beta 6 Xcode and it's not fixed.

Has anyone filed a radar? We're seeing this as well, and would like to follow-up on the fix.

We're sizing the cell itself, not the contentView.

I've been in contact with Apple about the issue but no official fix yet. There is an open radar to address this issue. We have found one workaround. First, make sure you set self.contentView.translatesAutoresizingMaskIntoConstraints = false. You will also need to override preferredLayoutAttributesFitting with the following code:


    override open func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        if #available(iOS 12.0, tvOS 12.0, *)
        {
            /*
            In iOS 12, there are issues with self-sizing cells. The fix is to call updateConstraintsIfNeeded() and turn on translatesAutoResizingMaskIntoConstraints as described in Apples release notes:
            "You might encounter issues with systemLayoutSizeFitting(_:) when using a UICollectionViewCell subclass that requires updateConstraints(). (42138227) — Workaround: Don't call the cell's setNeedsUpdateConstraints() method unless you need to support live constraint changes. If you need to support live constraint changes, call updateConstraintsIfNeeded() before calling systemLayoutSizeFitting(_:)."
            */
            self.updateConstraintsIfNeeded()
        
        
        return super.preferredLayoutAttributesFitting(layoutAttributes)

Having the same issue in production now and wondering how is the status.

I calculate manually using intrinsicContentSize of internal components as a workaround for now.

I just ran into this issue today with my code. It is thoroughly broken in iOS 12 release on Xcode 10.

My situation is further complicated as I've got a custom UICollectionViewLayout - which needs the height to calculate its layout.


The code works fine on an 11.4 simulator, but not for 12.1.


Is there a RDAR for this already?

A year later xcode 11 still has the bug. I resigned from coding because of that, if bug is non-fixable (none of your solution works especially when using objective-c) there is no point.