如何在 iOS 13 集合视图组合布局上隐藏标题

Jos*_*hpy 11 ios uicollectionview uicollectionviewlayout swift ios13

我在部署 iOS 13 组合布局时遇到了一个奇怪的问题。

我想隐藏集合视图标题和实现referenceSizeForHeaderInSection方法,但是referenceSizeForHeaderInSection无法在组合布局上工作

这是我的构图布局:

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
}()
Run Code Online (Sandbox Code Playgroud)

实施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
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试了referenceSizeForHeaderInSection但无法工作

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
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有任何想法或新方法来隐藏组合布局标题。

小智 0

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
        }
    }
Run Code Online (Sandbox Code Playgroud)

//添加elementKindSectionHeader 如果isFullScreen则返回nil

    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)
}
Run Code Online (Sandbox Code Playgroud)