2

I'm using the following to left align cells in a UICollectionView

class LeftAlignedFlowLayout: UICollectionViewFlowLayout {
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        guard let attributes = super.layoutAttributesForElements(in: rect) else { return nil }

        for attributes in attributes where attributes.representedElementCategory == .cell {
            attributes.frame = layoutAttributesForItem(at: attributes.indexPath).frame
        }
        return attributes
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes {
        let currentItemAttributes = super.layoutAttributesForItem(at: indexPath)!

        let layoutWidth = collectionView!.frame.width - sectionInset.left - sectionInset.right

        // Just left align the first item in a section
        if indexPath.item == 0 {
            currentItemAttributes.frame.origin.x = sectionInset.left
            return currentItemAttributes
        }

        let previousIndexPath = IndexPath(item: indexPath.item - 1, section: indexPath.section)
        let previousItemFrame = layoutAttributesForItem(at: previousIndexPath).frame

        let currentItemFrame = currentItemAttributes.frame
        let wholeRowFrameForCurrentItem = CGRect(x: sectionInset.left, y: currentItemFrame.minY, width: layoutWidth, height: currentItemFrame.height)

        // Left align the first item on a row
        if !previousItemFrame.intersects(wholeRowFrameForCurrentItem) {
            currentItemAttributes.frame.origin.x = sectionInset.left
            return currentItemAttributes
        }

        currentItemAttributes.frame.origin.x = previousItemFrame.maxX + minimumInteritemSpacing
        return currentItemAttributes, it's 
    }
}

Building for iOS 11 gives…

enter image description here

But for iOS 12 gives…

enter image description here

In iOS 11, layoutAttributesForElements(in) is called 3 times - the final time with the correct frame sizes. In iOS 12, it's only called twice, with frames being the estimated size.

Per this answer, https://stackoverflow.com/a/52148520/123632 I've tried invalidating the flow layout using the UICollectionView subclass below (and "brute-forcing" in viewDidAppear of the containing view controller), but the result is unchanged.

class LeftAlignedCollectionView: UICollectionView {

    private var shouldInvalidateLayout = false

    override func layoutSubviews() {
        super.layoutSubviews()
        if shouldInvalidateLayout {
            collectionViewLayout.invalidateLayout()
            shouldInvalidateLayout = false
        }
    }

    override func reloadData() {
        shouldInvalidateLayout = true
        super.reloadData()
    }
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160

3 Answers3

3

This is a known issue in iOS 12 (see UIKit section here: https://developer.apple.com/documentation/ios_release_notes/ios_12_release_notes)

The workaround is to either avoid calling setNeedsUpdateConstraints() or call updateConstraintsIfNeeded() before calling systemLayoutSizeFitting(_:).

ahbou
  • 4,710
  • 23
  • 36
1

I found a solution. Constraining the contentView to the cell fixed it…

override func awakeFromNib() {
    contentView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([contentView.leftAnchor.constraint(equalTo: leftAnchor),
                                 contentView.rightAnchor.constraint(equalTo: rightAnchor),
                                 contentView.topAnchor.constraint(equalTo: topAnchor),
                                 contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
0

I added this in the MyCustomCollectionViewCell file and it worked fine

override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {

    return contentView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: 1))
}
EnasAZ
  • 82
  • 6