我想知道是否有一种方法可以在使用组合布局的时候给一个组给予背景色。我想找一个可以直接使用的API,但是什么都没有找到。
62lalag41#
对于那些寻找答案的人,给你。
let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background( elementKind: FollowingCollectionViewController.sectionBackgroundDecorationElementKind) section.decorationItems = [sectionBackgroundDecoration] let layout = UICollectionViewCompositionalLayout(section: section) layout.register( SectionBackgroundDecorationView.self, forDecorationViewOfKind: FollowingCollectionViewController.sectionBackgroundDecorationElementKind)
oalqel3c2#
对于我来说,我想给整个部分添加一个背景视图,除了页眉。我没有在网上找到任何关于这个问题的东西。一个解决方案是给backgroundDecorationView添加contentInsets,除了supplementaryView的高度/宽度:
let itemSize = NSCollectionLayoutSize( widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) // Group let groupSize = NSCollectionLayoutSize( widthDimension: .fractionalWidth(1), heightDimension: .absolute(90)) let group = NSCollectionLayoutGroup.vertical( layoutSize: groupSize, subitem: item, count: 1) // Header let headerContentHeight: CGFloat = 15 let headerTopBottomInset: CGFloat = 7 let headerHeight = headerContentHeight+(headerTopBottomInset*2) let headerSize = NSCollectionLayoutSize( widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(headerHeight)) let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem( layoutSize: headerSize, elementKind: SectionHeaderView.identifier, alignment: .top) sectionHeader.contentInsets = NSDirectionalEdgeInsets(top: headerTopBottomInset, leading: 40, bottom: headerTopBottomInset, trailing: 40) // Background let backgroundItem = NSCollectionLayoutDecorationItem.background(elementKind: RoundedBGDecoratorView.identifier) let sectionInsets = NSDirectionalEdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20) // we are only adding top insets because background views get drawn starting from the top directly without taking the top inset value in calculations backgroundItem.contentInsets = NSDirectionalEdgeInsets( top: headerContentHeight+headerTopBottomInset+sectionInsets.top, leading: sectionInsets.leading, bottom: sectionInsets.bottom, trailing: sectionInsets.trailing) let section = NSCollectionLayoutSection(group: group) section.contentInsets = sectionInsets section.boundarySupplementaryItems = [sectionHeader] section.decorationItems = [backgroundItem]
2条答案
按热度按时间62lalag41#
对于那些寻找答案的人,给你。
oalqel3c2#
对于我来说,我想给整个部分添加一个背景视图,除了页眉。我没有在网上找到任何关于这个问题的东西。一个解决方案是给backgroundDecorationView添加contentInsets,除了supplementaryView的高度/宽度: