【发布时间】:2017-05-16 16:12:20
【问题描述】:
我正在使用带有流布局的 UICollectionView。我已经设置了标题,这是一个 UICollectionReusableView 的行为;
layout?.sectionHeadersPinToVisibleBounds = true
...
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath as IndexPath)
header.layer.zPosition = -1
return header
}
这会产生预期的效果,即当向上滚动单元格时,标题保持固定但位于常规单元格的后面。
但是,如果我尝试单击向顶部滚动的 UICollectionViewCell,即它在技术上覆盖了 UICollectionReusableView,则 UICollectionViewCell 的 didSelectItemAt 轻击事件将不再触发,直到我将其从标题所在的位置向下滚动。换句话说,UICollectionReusableView 会阻止点击手势,即使它的 zPosition 设置为 -1 并且不可见。
有没有人遇到过这个问题,你是如何解决的?
【问题讨论】:
-
我没有尝试过,但听起来
header.layer.zPosition = -1只是将layer推回去,而不是标题 view 本身(相当惊讶它完全有效,真的)。 -
标题“视图”仍然存在,因为您已将其固定。正如 DonMag 提到的,从视觉上看,您只是将其图层移回。似乎您根本不想要 header 而是其他类型的对象,您可以在布局中创建它。
-
我发现一个解决方法是设置
header.isUserInteractionEnabled = false- 但是如果标题包含一个 UIButton(我的)那么这将破坏按钮。我认为我的解决方案是将它们全部丢弃(停止固定标题)或在滚动时切换header.isUserInteractionEnabled = false。
标签: ios swift uicollectionview