Skip to content

Avoid overriding preferredLayoutAttributesFitting in every auto-sizing UICollectionViewCell #12

@astralchen

Description

@astralchen

Hi QuickLayout team,

First of all, thank you for open-sourcing QuickLayout.
We’ve been using it successfully in a UIKit project and really like the declarative layout style and performance benefits.

We are currently using QuickLayout inside UICollectionViewCell with UICollectionViewCompositionalLayout, where:
• Cell width is fixed (fractionalWidth = 1.0)
• Cell height is dynamic and content-driven
• Layout uses .estimated(...) height

To make auto-sizing work, we currently need to override
preferredLayoutAttributesFitting(:) in every custom cell and call sizeThatFits(), for example:

override func preferredLayoutAttributesFitting(
    _ layoutAttributes: UICollectionViewLayoutAttributes
) -> UICollectionViewLayoutAttributes {
    var attrs = layoutAttributes
    let targetSize = CGSize(
        width: layoutAttributes.size.width,
        height: .greatestFiniteMagnitude
    )
    let size = sizeThatFits(targetSize)
    attrs.size.height = ceil(size.height)
    return attrs
}

This works well, but it becomes repetitive when many different cells use QuickLayout.

Question

Is there a recommended or built-in approach to:
• Avoid overriding preferredLayoutAttributesFitting(_:) in every cell?
• Or provide a shared / automatic integration for auto-sizing cells when using UICollectionViewCompositionalLayout?

For example:
• A base UICollectionViewCell provided by QuickLayout
• A protocol or helper API
• Or a way for @quicklayout to hook into the sizing lifecycle automatically

We understand this might be a UIKit limitation, but wanted to ask if there is an officially recommended pattern or future plan for this use case.

Thanks again for the great library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions