Search code examples
swiftuicollectionviewuikituicollectionviewcompositionallayout

Swift Compositional Layout contentInsets.top is not working for header


I'm creating UICollectionViewCompositionalLayout with NSCollectionLayoutSection. I have section with assigned header. Setting contentInsets on section works as expected but setting contentInsets on header works only for horizontal alignment (leading and trailing are ok). The problem is that top and bottom insets are not working for header. Section has supplementariesFollowContentInsets set to false and header has extendsBoundary set to true. Any ideas why it is happening? I can post code but for now I did't because it's very long. Thanks!


Solution

  • According to the documentation it looks like it's not possible to do if you are using estimated when creating the layout size.

    The value of this property is ignored for any axis that uses an estimated value for its dimension. For more information, see estimated(_:).

    When you are creating CompositionalLayout instance you can pass the config with the spacing between the sections:

    let config = UICollectionViewCompositionalLayoutConfiguration()
    config.interSectionSpacing = 50
    
    return UICollectionViewCompositionalLayout(
        sectionProvider: { ... },
        configuration: config
    )