问题描述
我正在尝试使用自定义xib文件将标头添加到collectionView
.我使用实现UICollectionReusableView
的类创建了xib
文件.在collectionViewController
中,我这样注册了xib
文件:
I am trying to add header to collectionView
using custom xib file. I created the xib
file with class implementing UICollectionReusableView
.In collectionViewController
I registered the xib
file like this:
self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier)
然后在viewForSupplementaryElementOfKind
中我做了
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier, for: indexPath) as! HCollectionReusableView
和调整大小
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 100, height: 50)
}
我收到错误消息:无法将NIB捆绑装入.任何缺少的代码?
I am getting error: Could not load NIB in bundle.any missing code ?
HCollectionReusableView类:
HCollectionReusableView class:
class HCollectionReusableView: UICollectionReusableView {
static var nibName : String
{
get { return "headerNIB"}
}
static var reuseIdentifier: String
{
get { return "headerCell"}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
推荐答案
您需要像这样调用viewForSupplementaryElementOfKind
:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as! HCollectionReusableView
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
//do other header related calls or settups
return reusableview
default: fatalError("Unexpected element kind")
}
}
这样,您可以初始化并显示标题
This way you can initialise and show the header
设置UICollectionViewHeader框架的另一种方法是通过扩展UICollectionViewDelegateFlowLayout
这样:
Another way of setting the UICollectionViewHeader frame is by extending UICollectionViewDelegateFlowLayout
like this:
extension UIViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100) //add your height here
}
}
这消除了呼叫的必要:
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
在上面提到的
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView")
Swift 4.1更新
UICollectionElementKindSectionHeader
已重命名为UICollectionView.elementKindSectionHeader
这篇关于将自定义标题添加到集合视图swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!