【问题标题】:UICollectionView header view like table view header (not the section header)UICollectionView 标题视图类似于表视图标题(不是节标题)
【发布时间】:2018-06-13 08:19:22
【问题描述】:

是否可以像 UITableView headerView 一样创建 UICollectionView 标题视图?我的意思是整个集合视图的标题视图,而不是每个部分的重复视图。就像我想要的图片1,现在我已经完成的图片。

【问题讨论】:

标签: swift 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)

【讨论】:

  • 但是这里的滚动不适用于topImageView,这意味着当我滚动collectionview时topImageView不会向右滚动。
【解决方案2】:

当我想要实现这一点时,我使用了一种解决方法。设置标题大小时,我会在设置之前检查节号。如果是第一部分,我会相应地设置高度 - 否则我将高度设置为 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 的标题。如果没有部分标题,也许你的答案可以工作。其他时候,这个函数中没有section参数。所以...
【解决方案3】:

像下面这样快速

注册标题视图

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)

否则委托方法不会被调用

【讨论】:

  • 我认为你的答案不适合我。首先,我同时拥有部分标题和整个 collectionView 的标题。如果没有部分标题,也许你的答案可以工作。其他时候,这个函数中没有section参数。所以...
  • @C.Hugh 一切都无法满足我们的需求。我们必须根据我们的要求对其进行定制。说明您遇到的问题会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-20
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多