【问题标题】:Swift - Change height of CollectionView HeaderSwift - 更改 CollectionView Header 的高度
【发布时间】:2019-01-14 20:26:22
【问题描述】:

我想更改CollectionViewController 的标题高度。

我已经知道我可以使用这样的代码:

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

我想这样做,但直接在 UICollectionReusableView(标题视图)中。

是否有任何功能允许这样做?

【问题讨论】:

    标签: ios swift swift3 uicollectionview resize


    【解决方案1】:

    要动态更改标题大小,请使用 headerReferenceSize UICollectionViewFlowLayout 属性。

    let collectionViewLayout = UICollectionViewFlowLayout()
    collectionViewLayout.headerReferenceSize = CGSize(width: view.frame.size.width, height: 80)
    

    确保包含 referenceSizeForHeaderInSection 委托方法以使其工作。

    【讨论】:

      【解决方案2】:

      collectionview 中的标题与 tableview 不同,因为您需要创建不同类型的单元格,此单元格名为 UICollectionReusableView,创建一个具有此名称的类,然后在您的 collectionview 中选中此选项: enter image description here

      然后你可以用这个函数在代码上调用它:

      override func collectionView(_ collectionView: UICollectionView,
                                   viewForSupplementaryElementOfKind kind: String,
                                   at indexPath: IndexPath) -> UICollectionReusableView {
        //1
        switch kind {
        //2
        case UICollectionElementKindSectionHeader:
          //3
          let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                             withReuseIdentifier: "UICollectionReusableViewID",
                             for: indexPath) as! UICollectionReusableViewClass
      //Customize
      
          return headerView
        default:
          //4
          assert(false, "Unexpected element kind")
        }
      }
      

      所有学分 RayWenderLich 教程:https://www.raywenderlich.com/1404-uicollectionview-tutorial-reusable-views-selection-and-reordering

      【讨论】:

        【解决方案3】:

        您需要添加UICollectionViewDelegateFlowLayout

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-24
          • 1970-01-01
          • 2021-01-23
          • 2015-11-15
          相关资源
          最近更新 更多