【发布时间】:2018-06-13 08:19:22
【问题描述】:
是否可以像 UITableView headerView 一样创建 UICollectionView 标题视图?我的意思是整个集合视图的标题视图,而不是每个部分的重复视图。就像我想要的图片1,现在我已经完成的图片。
【问题讨论】:
是否可以像 UITableView headerView 一样创建 UICollectionView 标题视图?我的意思是整个集合视图的标题视图,而不是每个部分的重复视图。就像我想要的图片1,现在我已经完成的图片。
【问题讨论】:
我现在有了解决方案。在 collectionView 中添加一个子视图,并将 collectionView contentInset 设置在 topImageView 下方,如下所示。
topImageView.frame = CGRect(x: 5*SCREEN_SCALE, y: -125*SCREEN_SCALE, width: 285*SCREEN_SCALE, height: 120*SCREEN_SCALE)
collectionView.addSubview(topImageView)
collectionView.contentInset = UIEdgeInsets(top: 130*SCREEN_SCALE, left: 0, bottom: 0, right: 0)
【讨论】:
当我想要实现这一点时,我使用了一种解决方法。设置标题大小时,我会在设置之前检查节号。如果是第一部分,我会相应地设置高度 - 否则我将高度设置为 0,因此它不可见
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section == 0 {
return CGSize(width: view.frame.width, height: 35)
} else {
return CGSize(width: view.frame.width, height: 0)
}
}
【讨论】:
像下面这样快速
注册标题视图
collectionView.registerClass(HeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView")
在 UICollectionViewDelegate 中
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
if section == 0 {
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView", forIndexPath: indexPath)
headerView.frame.size.height = 100
return headerView }
else {
return nil
}
}
重要的是您要为流布局提供标题大小
flowLayout.headerReferenceSize = CGSize(width: self.collectionView.frame.width, height: 100)
否则委托方法不会被调用
【讨论】: