Ios 如何使用swift在集合视图中创建页眉和页脚

Ios 如何使用swift在集合视图中创建页眉和页脚,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,如何在swift的集合视图中同时生成页眉和页脚 我试图将页眉和页脚组合在一起,但它不断崩溃,我找不到swift教程来理解它 我不知道如何返回两个视图的补充视图,而只是返回一个 我在故事板上设置了它们(类+标识符) 错误: 标识符为1的UICollectionElementKindCell-必须为标识符注册nib或类,或连接情节提要中的原型单元 我希望有人能提供帮助。您可以制作一个UICollectionViewController来处理UICollectionView并在界面生成器中激活页脚和页

如何在swift的集合视图中同时生成页眉和页脚

我试图将页眉和页脚组合在一起,但它不断崩溃,我找不到swift教程来理解它

我不知道如何返回两个视图的补充视图,而只是返回一个

我在故事板上设置了它们(类+标识符)

错误:
标识符为1的UICollectionElementKindCell-必须为标识符注册nib或类,或连接情节提要中的原型单元


我希望有人能提供帮助。

您可以制作一个
UICollectionViewController
来处理
UICollectionView
并在界面生成器中激活页脚和页眉部分,然后您可以使用以下方法在
UICollectionView
中预览添加的两个部分:

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionView.elementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath)

        headerView.backgroundColor = UIColor.blue
        return headerView

    case UICollectionView.elementKindSectionFooter:
        let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath)

        footerView.backgroundColor = UIColor.green
        return footerView

    default:

        assert(false, "Unexpected element kind")
    }
在上面的代码中,我将页脚和页眉的
标识符设置为
页眉
页脚
,例如,您可以根据需要进行操作。如果要创建自定义页眉或页脚,则需要为每个页眉或页脚创建一个子类
UICollectionReusableView
,并根据需要对其进行自定义

您可以通过以下方式在Interface Builder或代码中注册自定义页脚和页眉类:

registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
更新为Swift 3+

第1步:

在视图控制器类中,注册要用作页眉、页脚或两者的类:

let collectionViewHeaderFooterReuseIdentifier = "MyHeaderFooterClass"
第二步:

如果使用xib,请使用:

collectionView.register(UINib(nibName: collectionViewHeaderFooterReuseIdentifier bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier:collectionViewHeaderFooterReuseIdentifier)

collectionView.register(UINib(nibName: collectionViewHeaderFooterReuseIdentifier bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier:collectionViewHeaderFooterReuseIdentifier)
如果不使用xib:

collectionView.register(MyHeaderFooterClass.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: collectionViewHeaderFooterReuseIdentifier)

collectionView.register(MyHeaderFooterClass.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: collectionViewHeaderFooterReuseIdentifier)
第三步:

创建自定义页眉/页脚类,实现如下所示:

import UIKit

class MyHeaderFooterClass: UICollectionReusableView {

 override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = UIColor.purple

    // Customize here

 }

 required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

 }
}
第4步:如果不使用xib,请忽略

  • 创建新的空xib:“文件-->新文件-->空”

  • 将其命名为与类完全相同的名称。在本例中:“MyHeaderFooterClass”

  • 将集合可重用视图添加到xib
  • 单击该对象,选择标识检查器并将该对象的类更改为“MyHeaderFooterClass”
第五步: -通过委托方法支持集合视图中的新单元格:

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

    switch kind {

    case UICollectionElementKindSectionHeader:
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: collectionViewHeaderFooterReuseIdentifier, for: indexPath)

        headerView.backgroundColor = UIColor.blue
        return headerView

    case UICollectionElementKindSectionFooter:
        let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: collectionViewHeaderFooterReuseIdentifier, for: indexPath)

        footerView.backgroundColor = UIColor.green
        return footerView

    default:
        assert(false, "Unexpected element kind")
    }
}
第6步: 控制柄大小/使其显示:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 180.0)
}
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 60.0, height: 30.0)
}

为了补充其余的答案,请不要忘记为页眉/页脚视图分配空间,否则将不会调用
collectionView:viewforsupplementalelementofkind:atIndexPath


通过在collectionView数据源中实现
collectionView:layout:ReferenceSizeForHeaderInstruction

解决方案

class CustomFlowLayout: UICollectionViewFlowLayout {

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let attributesForElementsInRect = super.layoutAttributesForElements(in: rect)
        var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()

        for attributes in attributesForElementsInRect! {

            if !(attributes.representedElementKind == UICollectionElementKindSectionHeader
                || attributes.representedElementKind == UICollectionElementKindSectionFooter) {

                // cells will be customise here, but Header and Footer will have layout without changes.
            }

            newAttributesForElementsInRect.append(attributes)
        }

        return newAttributesForElementsInRect
    }
}


class YourViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let headerNib = UINib.init(nibName: "HeaderCell", bundle: nil)
        collectionView.register(headerNib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell")

        let footerNib = UINib.init(nibName: "FooterCell", bundle: nil)
        collectionView.register(footerNib, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "FooterCell")
    }


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

        switch kind {
        case UICollectionElementKindSectionHeader:
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderCell", for: indexPath) as! HeaderCell
            return headerView
        case UICollectionElementKindSectionFooter:
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FooterCell", for: indexPath) as! FooterCell
            return footerView
        default:
            return UICollectionReusableView()
        }
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 45)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 25)
    }
}

在使用@mobilecat代码后,您应该使用此函数来显示页眉和页脚

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 180.0)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 60.0, height: 30.0)
    }

请注意,viewController必须实现UICollectionViewDelegateFlowLayout,否则将无法调用这些方法

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

}

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

}

除了以上所有答案之外

激活页脚和页眉部分的步骤

选择您的集合,然后选择属性检查器并检查页脚和页眉部分

像照片里一样


如果需要为页眉和页脚保留空白,请扩展
UICollectionViewDelegateFlowLayout
,并使用此代码相应地设置页眉或页脚

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let footerValue = 150
    let headerValue = 150
    collectionView.contentInset = UIEdgeInsets(top: headerValue, left: 0, bottom: footerValue, right: 0)
}

Xcode 11+&iOS 13+

我正在使用Xib创建页眉和页脚视图

  • 像这样为页眉和页脚创建一个Xibs和类文件(与页脚类相同)&同时为这两个类创建xib视图
  • 像这样注册Xib细胞
  • 页眉和页脚视图方法
  • 对于“视图高度”方法,请同时使用
  • 完成CollectionView中页眉和页脚的所有必要要求

  • 在@mobilecat detailed answer之后,如果您使用的是合成集合视图,请在创建布局时添加以下行:

    let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44))
    let headerElement = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
    
    然后将
    headerElement
    设置为您的分区,如下所示:

     let section = NSCollectionLayoutSection(group: group)
     section.boundarySupplementaryItems = [headerElement]
    
    完整的代码片段是:

    private func createCompositionalLayout() -> UICollectionViewCompositionalLayout {
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalHeight(1.0),
                                              heightDimension: .fractionalHeight(0.2))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
    
        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                               heightDimension: .fractionalWidth(1.0))
        
        let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
        
        let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44))
        let headerElement = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
    
        let section = NSCollectionLayoutSection(group: group)
        section.boundarySupplementaryItems = [headerElement]
    
    
        let layout = UICollectionViewCompositionalLayout(section: section)
        return layout
      }
    

    请输入有关标识符为1的crashUICollectionElementKindCell的更多信息-必须为标识符注册nib或类,或者将情节提要的标识符页脚中的原型单元格与UICollectionReusableView的页脚单元格连接,标识符页眉与的页眉单元格连接UICollectionReusableView@marrios你登记了吗在代码或Interface Builder中为自定义页眉和页脚创建类???请参阅更新的答案以了解如何在代码中为自定义页眉或页脚注册类您是否有要共享的示例应用程序?嘿,我们如何在collectionView的页脚中添加内容?例如,我必须在uicollection的页脚中添加uilabel。从你的方法,我可以创建页脚,但不知道如何添加它。请帮助我,为了做到这一点,您需要遵守
    UICollectionViewDelegateFlowLayout
    即使对于Swift 5也是完美的&如果您以编程方式创建所有内容的话
    collectionView.register(UINib(nibName: "HeaderViewCV", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HeaderViewCV") //elementKindSectionFooter for footerview
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
       switch kind {
                    
       case UICollectionView.elementKindSectionHeader:
                let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderViewCV", for: indexPath)
                return headerView
                
        case UICollectionView.elementKindSectionFooter:
                let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "FooterViewCV", for: indexPath)
                return footerView
                
         default:
                assert(false, "Unexpected element kind")
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: self.collectionView.frame.width, height: 55)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: self.collectionView.frame.width, height: 67)
    }
    
    let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44))
    let headerElement = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
    
     let section = NSCollectionLayoutSection(group: group)
     section.boundarySupplementaryItems = [headerElement]
    
    private func createCompositionalLayout() -> UICollectionViewCompositionalLayout {
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalHeight(1.0),
                                              heightDimension: .fractionalHeight(0.2))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
    
        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                               heightDimension: .fractionalWidth(1.0))
        
        let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
        
        let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44))
        let headerElement = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
    
        let section = NSCollectionLayoutSection(group: group)
        section.boundarySupplementaryItems = [headerElement]
    
    
        let layout = UICollectionViewCompositionalLayout(section: section)
        return layout
      }