【问题标题】:UICollectionView hide/prevent cell scrolling behind Sticky HeaderUICollectionView 隐藏/防止单元格在 Sticky Header 后面滚动
【发布时间】:2017-08-16 06:46:18
【问题描述】:

我有一个带有粘性标题的集合视图

 flowLayout.sectionHeadersPinToVisibleBounds = true

我的问题是标题的上半部分是半透明的,当我向上滚动单元格时,我可以看到单元格在标题后面滚动。

我想将单元格视图的一部分隐藏在标题后面。在我附上的图片中,我不想在红色后面看到绿色。其余的行为我想保持原样。

我需要这个的原因是我在后面有一张壁纸图片需要显示

@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView.alwaysBounceVertical = true
    collectionView.register(UINib.init(nibName: EXAMPLE_CELL_REUSE_ID, bundle: nil), forCellWithReuseIdentifier: EXAMPLE_CELL_REUSE_ID)
    collectionView.register(UINib.init(nibName: EXAMPLE_HEADER_REUSE_ID, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: EXAMPLE_HEADER_REUSE_ID)

    if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
      flowLayout.headerReferenceSize = CGSize(width: 400, height: 100)
      flowLayout.sectionHeadersPinToVisibleBounds = true
  }
}




func numberOfSections(in collectionView: UICollectionView) -> Int {
    return sections.count;
 }

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1 //self.sections[section].1;
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let exampleCell = collectionView.dequeueReusableCell(withReuseIdentifier: EXAMPLE_CELL_REUSE_ID, for: indexPath) as! MyCellCollectionViewCell;

    exampleCell.headerLabel.text = "Cell"
    exampleCell.backgroundColor = UIColor.green

    return exampleCell;
}


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionElementKindSectionHeader {
      if let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: EXAMPLE_HEADER_REUSE_ID, for: indexPath) as? ExampleHeader {
       //  header.backgroundColor = UIColor(red: 1.0, green: 0, blue: 0, alpha: 0.5)
        return header
      } else {
        return UICollectionReusableView()

      }
    }
    return UICollectionReusableView()
}

我认为这里的问题可能相似,但没有任何回应,并且不太清楚是否是同一个问题。 Transparent sticky header ui collectionview don't show cells underneath

【问题讨论】:

  • 显示你使用过的代码。
  • 代码是标准的,除了sectionHeadersPinToVisibleBounds。我还是发布了它
  • 你找到解决办法了吗?

标签: ios swift uicollectionview uicollectionviewlayout


【解决方案1】:

这对我有用:

let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    layout.sectionHeadersPinToVisibleBounds = true
    layout.sectionInsetReference = .fromSafeArea
    view.backgroundColor = .white
    collectionView.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: 
    view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor)

【讨论】:

    【解决方案2】:

    您需要为位于标题视图下的所有单元格应用掩码。在scrollViewDidScroll上使用

    教程:https://medium.com/@peteliev/layer-masking-for-beginners-c18a0a10743

    【讨论】:

      【解决方案3】:

      我已经创建了一个像您这样的非常简单的设置,并且运行良好。

      class ViewController: UIViewController, UICollectionViewDataSource
      {
          @IBOutlet weak var collectionView: UICollectionView!
      
          override func viewDidLoad()
          {
              super.viewDidLoad()
              self.collectionView.alwaysBounceVertical = true
      
              if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
              {
                  flowLayout.headerReferenceSize = CGSize(width: 400, height: 100)
                  flowLayout.sectionHeadersPinToVisibleBounds = true
              }
          }
      
          func numberOfSections(in collectionView: UICollectionView) -> Int
          {
              return 10
          }
      
          func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
          {
              return 1
          }
      
          func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
          {
              return collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
          }
      
          func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
          {
              if kind == UICollectionElementKindSectionHeader
              {
                  return collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "rview", for: indexPath)
              }
              return UICollectionReusableView()
          }
      }
      

      【讨论】:

      • 什么效果好?我的集合视图有效,我的问题是如何有一个透明的标题,而看不到它后面的单元格。
      • 你是如何让标题透明的?我的意思是这样做的代码
      • 哦..真的很抱歉..我没有完全理解你的问题。此链接stackoverflow.com/questions/33010199/… 可能会对您有所帮助。
      • 谢谢,但完全不相关
      • 我认为这种功能是不可能的。我也搜索了解决方案,但找不到。
      猜你喜欢
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 2018-10-18
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多