我使用UICollectionView
与一个独特的部分来拆分大视图的逻辑。这个有一个HeaderView
带有图片,我试着让这个标题变得粘稠和有弹性(当你向下滚动标题变小,直到只是一个酒吧,坚持顶部,当你向上滚动并反弹时,标题来回到图片和舒展)。UICollectionViewFlowLayout子类粘滞和伸展标题
我已阅读并看很多教程,并设法使我自己的实现,但每一次,同样的行为发生了:
细胞不会再滚动,头向下滑动。
而且每次我打电话:
collectionViewLayout.invalidateLayout()
集合视图滚动回顶部没有动画。
下面,只有有弹性的部分!
这里我CustomCollectionViewFlowLayout代码:
class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let layoutAttributes = super.layoutAttributesForElements(in: rect),
let collectionView = collectionView else {
return nil
}
let offset = collectionView.contentOffset
if offset.y < 0 {
let deltaY = abs(offset.y)
for attributes in layoutAttributes {
if attributes.representedElementKind == UICollectionElementKindSectionHeader {
var frame = attributes.frame
frame.size.height = max(0, headerReferenceSize.height + deltaY)
frame.origin.y = frame.minY - deltaY
attributes.frame = frame
}
}
}
return layoutAttributes
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
在这里,我如何与CollectionViewController链接它:
override func viewDidLoad() {
super.viewDidLoad()
if let flowLayout = collectionViewLayout as? PINPropertyDetailsCollectionViewFlowLayout {
let side = collectionView?.frame.width ?? 375
flowLayout.headerReferenceSize = CGSize(width: side, height: side)
flowLayout.estimatedItemSize = CGSize(width: side, height: 1)
}
collectionView?.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)
}
我已经尝试使用:
flowLayout.sectionHeadersPinToVisibleBounds = true
以及与没有更多的结果:什么都没有(细胞或头)滚动了...
我使用的Xcode 9(测试4),其编码的iOS 9 & 10.
感谢反馈:)