将自定义标题添加到集合视图 swift [英] add custom header to collection view swift
问题描述
我正在尝试使用自定义 xib 文件将标题添加到 collectionView
.我用实现 UICollectionReusableView
的类创建了 xib
文件.在 collectionViewController
我像这样注册了 xib
文件:
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) ->CGS 大小 {返回 CGSize(宽度:100,高度:50)}
我收到错误消息:无法在捆绑包中加载 NIB.任何缺少的代码?
HCollectionReusableView 类:
类 HCollectionReusableView: UICollectionReusableView {静态变量 nibName : 字符串{得到{返回headerNIB"}}静态变量重用标识符:字符串{获取{返回headerCell"}}覆盖 func awakeFromNib() {super.awakeFromNib()//初始化代码}
}
你需要像这样调用 viewForSupplementaryElementOfKind
:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) ->UICollectionReusableView {切换种类{案例 UICollectionElementKindSectionHeader:让 reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as!HCollectionReusableViewreusableview.frame = CGRect(0, 0, self.view.frame.width, headerHight)//做其他与头部相关的调用或设置返回可重用视图默认值:fatalError("意外的元素种类")}}
这样你可以初始化并显示标题
另一种设置 UICollectionViewHeader 框架的方法是像这样扩展 UICollectionViewDelegateFlowLayout
:
扩展 UIViewController: UICollectionViewDelegateFlowLayout {func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) ->CGS 大小 {return CGSize(width: collectionView.frame.width, height: 100)//在这里添加你的高度}}
这消除了调用的需要:
reusableview.frame = CGRect(0, 0, self.view.frame.width, headerHight)
在上面提到
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) ->UICollectionReusableView
<块引用>
记得在你初始化你的 UICollectionView
之后注册 HeaderView:
collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView")
Swift 4.1 更新
UICollectionElementKindSectionHeader
已重命名为 UICollectionView.elementKindSectionHeader
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)
and after that in viewForSupplementaryElementOfKind
I did
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier, for: indexPath) as! HCollectionReusableView
and for sizing
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 100, height: 50)
}
I am getting error: Could not load NIB in bundle. any missing code ?
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
}
}
You need to call viewForSupplementaryElementOfKind
like this:
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
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
}
}
This removes the need to call :
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
in the above mentioned
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
Remember to register the HeaderView after you initialise your
UICollectionView
by calling:
collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView")
Swift 4.1 Update
UICollectionElementKindSectionHeader
has been renamed to UICollectionView.elementKindSectionHeader
这篇关于将自定义标题添加到集合视图 swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!