UICollectionView header not showing

前端 未结 3 1403
感动是毒
感动是毒 2021-01-31 14:21

I\'m working on a project that uses an UICollectionView to show several albums. The items show fine, but now I want to show an header above the first section.

3条回答
  •  遥遥无期
    2021-01-31 15:09

    for SWIFT 3 & SWIFT 4

        self.collectionView.register(UINib(nibName: "SectionCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionCollectionReusableView")
    
    
    
        self.collectionView.fs_width = self.collectionView.bounds.width
    
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 70, left: 40, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.estimatedItemSize = CGSize(width: 350, height: 140)
        layout.scrollDirection = .horizontal
        layout.headerReferenceSize = CGSize(width: self.collectionView.bounds.size.width, height: 60)
        layout.sectionHeadersPinToVisibleBounds = true
        self.collectionView!.collectionViewLayout = layout
    

    You have to add this to ViewDidLoad, and notice the

    layout.headerReferenceSize = CGSize(width: self.collectionView.bounds.size.width, height: 60)
    

    This will make the header Section Layouts

    and Thanks @Guido Hendriks, and all I have got the insight from their answer as well

提交回复
热议问题