Constraint preferredLayoutAttributes width

201 Views Asked by At

I work on a custom subclass of UICollectionViewLayout with fixed columns and vertically growing items similar to Trello. Each item is basically a view that contains a few vertically stacked labels.

Currently what I observe is that shouldInvalidateLayout(forPreferredLayoutAttributes:) is being called with the computed by auto layout size but the size is rather fittingSize.

I wonder if there is any way to constraint the preferredLayoutAttributes width since I have fixed width columns, and let it calculate the height for me?

override func shouldInvalidateLayout(forPreferredLayoutAttributes preferredAttributes: NSCollectionViewLayoutAttributes, withOriginalAttributes originalAttributes: NSCollectionViewLayoutAttributes) -> Bool {
  if preferredAttributes.size != originalAttributes.size {
    return true
  }
  return false
}

override func invalidationContext(forPreferredLayoutAttributes preferredAttributes: NSCollectionViewLayoutAttributes, withOriginalAttributes originalAttributes: NSCollectionViewLayoutAttributes) -> NSCollectionViewLayoutInvalidationContext {
  let invalidationContext = super.invalidationContext(
    forPreferredLayoutAttributes: preferredAttributes, 
    withOriginalAttributes: originalAttributes
  ) as! WeekViewInvalidationContext

  // save computed attributes to apply them later during invalidateLayout
  invalidationContext.updatedPreferredLayoutAttributes = preferredAttributes

  return invalidationContext
}
0

There are 0 best solutions below