Reputation: 600
I encounter a wonder point when deploy the iOS 13 Compositional Layout.
I want to hide collection view header and implement referenceSizeForHeaderInSection
method, however the referenceSizeForHeaderInSection
can not work on Compositional Layout
This my Compositional Layout:
lazy var collectionViewLayout: UICollectionViewLayout = {
// item layout deploy
let cellItemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(0.2))
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.33), heightDimension: .fractionalHeight(1.0))
let cellItem = NSCollectionLayoutItem(layoutSize: cellItemSize)
let item = NSCollectionLayoutItem(layoutSize: itemSize)
cellItem.contentInsets = NSDirectionalEdgeInsets(top: 4, leading: 4, bottom: 4, trailing: 4)
item.contentInsets = NSDirectionalEdgeInsets(top: 4, leading: 4, bottom: 4, trailing: 4)
// group layout deploy
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(0.2))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 3)
let containerGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(215))
let containerGroup = NSCollectionLayoutGroup.vertical(layoutSize: containerGroupSize, subitems: [cellItem, group, group, group, group])
// section layout deploy
let section = NSCollectionLayoutSection(group: containerGroup)
section.contentInsets = NSDirectionalEdgeInsets(top: 4, leading: 0, bottom: 4, trailing: 0)
// Headers layout deploy
let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(18))
let header = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: SupplementaryViewOfKind.Header.rawValue, alignment: .top)
section.boundarySupplementaryItems = [header]
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}()
Implement viewForSupplementaryElementOfKind
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard
kind == SupplementaryViewOfKind.Header.rawValue,
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: XXXHeader.identifier, for: indexPath) as? XXXHeader
else { return UICollectionReusableView() }
if isFullScreen {
// customer header with titleLabel property
header.titleLabel.text = segmentedControl.titleForSegment(at: indexPath.section)
} else {
header.titleLabel.text = nil
}
}
Then I tried into referenceSizeForHeaderInSection
but could't work
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if isFullScreen {
return CGSize(width: collectionView.frame.width, height: 18)
} else {
return CGSize.zero
}
}
Is there any idea or new way to hide Compositional Layout header.
Upvotes: 12
Views: 5109
Reputation: 477
When you are using UICollectionViewCompositionalLayout
, you can hide the footer like this:
/// Hides the footer of the collectionView (animated)
func hideFooter() {
let compLayout = collectionView.collectionViewLayout as? UICollectionViewCompositionalLayout
UIView.animate(withDuration: 0.4) {
compLayout?.configuration = UICollectionViewCompositionalLayoutConfiguration()
}
}
Prerequisite (you need to set the footer like this):
let layout = UICollectionViewCompositionalLayout { ... }
let config = UICollectionViewCompositionalLayoutConfiguration()
let size = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(10))
let footer = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: size,
elementKind: UICollectionView.elementKindSectionFooter,
alignment: .bottom, absoluteOffset: CGPoint(x: 0, y: 0))
config.boundarySupplementaryItems = [footer]
layout.configuration = config
Upvotes: 0
Reputation: 1913
For now there seems like there is no other way to do it other than creating your own protocol for figuring out whether headers should be displayed in a section. Luckily, the UICollectionViewCompositionalLayout
constructor gives us the section index so it's quite easy to do. Simply do not create any supplementary views for a section without them.
protocol CollectionViewLayoutProviderDelegate: class {
func layoutProvider(_ provider: CollectionViewLayoutProvider, shouldDisplaySupplementaryViewIn section: Int) -> Bool
}
class CollectionViewLayoutProvider {
weak var delegate: CollectionViewLayoutProviderDelegate?
init(delegate: CollectionViewLayoutProviderDelegate?) {
self.delegate = delegate
}
private func prepareSupplementaryViews(section: Int) -> [NSCollectionLayoutBoundarySupplementaryItem] {
if let delegate = delegate,
!delegate.layoutProvider(self, shouldDisplaySupplementaryViewIn: section) {
// Return empty array to avoid displaying headers
return []
}
// Configure header if you need to
}
private func prepareSection(section: Int, group: NSCollectionLayoutGroup) -> NSCollectionLayoutSection {
let layoutSection = NSCollectionLayoutSection(group: group)
layoutSection.boundarySupplementaryItems = prepareSupplementaryViews(section: section)
return layoutSection
}
}
Then, in your view controller
private lazy var layoutProvider = CollectionViewLayoutProvider(delegate: self)
extension YouViewController: CollectionViewLayoutProviderDelegate {
func layoutProvider(_ provider: CollectionViewLayoutProvider, shouldDisplaySupplementaryViewIn section: Int) -> Bool {
return viewModel.shouldDisplayHeader(for: section) != nil
}
}
Upvotes: 2
Reputation: 469
When you're generating layout, the index can be passed in
func generateLayout() -> UICollectionViewLayout {
let layout = UICollectionViewCompositionalLayout {(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
// do your layout stuff here. With sectionIndex, you can layout differently for each section
}
return layout
}
Upvotes: 3
Reputation: 185
func configureDiffableDataSource() {
let dataSource = UICollectionViewDiffableDataSource<TripSection, TripItem>(collectionView: collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, item: TripItem) -> UICollectionViewCell? in
let cell = collectionView.dequeueCell(ofType: SmallTableViewCell.self, for: indexPath)
cell.fillWithData(tip)
return cell
}
}
//ADD elementKindSectionHeader return nil if isFullScreen
dataSource.supplementaryViewProvider = { [weak self] (collectionView: UICollectionView, kind: String, indexPath: IndexPath) -> UICollectionReusableView? iln
guard let itamSequence = self?.dataSource?.itemIdentifier(for: indexPath) else { return nil }
guard let section = self?.dataSource?.snapshot().sectionIdentifier(containingItem: itamSequence) else { return nil }
let boardHeader = collectionView.dequeueReusableView(ofType: BoardSupplementaryView.self, forKind: UICollectionView.elementKindSectionHeader, for: indexPath)
return boardHeader
}
}
self.dataSource = dataSource
updateSnapshot(animated: false)
}
Upvotes: 0