imzyf / ios-swift-learning-notes

📝 iOS Swift Learning Notes - see Issues
MIT License
0 stars 0 forks source link

UICollectionView header footer #67

Closed imzyf closed 6 years ago

imzyf commented 6 years ago

注册

var nib = UINib(nibName: self.headerName, bundle: nil)
        self.collectionView.register(nib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: self.headerName)

遍历

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:
            switch indexPath.section {
            case 1:
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.headerName, for: indexPath)

                return headerView
            case 2:
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.headerBroderName, for: indexPath)

                return headerView
            default:
                return UICollectionReusableView()
            }
        default:
            return UICollectionReusableView()
        }
    }
imzyf commented 6 years ago

尺寸

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

        switch section {
        case 1:
            return CGSize(width: collectionView.bounds.width, height: 31)
        case 2:
            return CGSize(width: collectionView.bounds.width, height: 26)
        default:
            return CGSize.zero
        }
    }
imzyf commented 6 years ago

合并