Ios Swift:UICollectionView标题在滚动时消失
这是我在Ios Swift:UICollectionView标题在滚动时消失,ios,swift,uicollectionview,uicollectionviewlayout,Ios,Swift,Uicollectionview,Uicollectionviewlayout,这是我在StackOverflow上的第一篇帖子,我是Swift的新手 我用UICollectionView实现了类似android的折叠工具栏布局,并用自定义CollectionViewFlowLayout动态更改了标题的高度。 但当我向上滚动时,我的标题消失了,我发现它在某个高度消失了 有没有人有过相同的想法或遇到过相同的问题 我曾尝试将headerReferenceSize或sectionHeaderPinToVisibleBound添加到我的自定义布局中,但前者失败,后者修复了标题的高度
StackOverflow
上的第一篇帖子,我是Swift的新手
我用UICollectionView
实现了类似android的折叠工具栏布局,并用自定义CollectionViewFlowLayout动态更改了标题的高度。
但当我向上滚动时,我的标题消失了,我发现它在某个高度消失了
有没有人有过相同的想法或遇到过相同的问题
我曾尝试将headerReferenceSize
或sectionHeaderPinToVisibleBound
添加到我的自定义布局中,但前者失败,后者修复了标题的高度
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributes = super.layoutAttributesForElements(in: rect)
layoutAttributes?.forEach({ (attribute) in
if attribute.representedElementKind == UICollectionView.elementKindSectionHeader{
guard let collectionView = collectionView else {return}
let contentOffsetY = collectionView.contentOffset.y
let width = collectionView.frame.width
let height = attribute.frame.height - contentOffsetY
let titleHeight = UIScreen.main.bounds.height/10
let testHeight = attribute.frame.height - titleHeight
if contentOffsetY > testHeight {
attribute.frame = CGRect(x: 0, y: contentOffsetY, width: width, height: titleHeight)
headerReferenceSize = CGSize(width: width, height: titleHeight)
} else {
attribute.frame = CGRect(x: 0, y: contentOffsetY, width: width, height: height)
}
}
})
return layoutAttributes
}
上面是问题演示的链接,很抱歉,我不知道如何将视频粘贴到StackOverflow上。之所以会发生这种情况,是因为在向上滚动一定时间后,layoutAttributesForElements会使用不同的CGRect调用 我也有同样的效果,可以通过在viewDidLoad()中设置sectionHeadersPinToVisibleBounds=true来消除它 但不再为滚动视图的每次移动调用layoutAttributesForElements(in:)。如果只需要设置标题的布局,最好使用layoutAttributesForElements(ofKind:at:)如下所示:
我和你有同样的案子。不知道它为什么消失了:(
if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
layout.sectionInset = .init(top: kPadding, left: kPadding, bottom: kPadding, right: kPadding)
layout.minimumLineSpacing = 10
layout.headerReferenceSize = .init(width: self.view.frame.width, height: ProfileViewHeader.kHeight)
layout.sectionHeadersPinToVisibleBounds = true
}
override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
if let attributes = super.layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath) {
if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
guard let collectionView = collectionView else { return attributes }
let contentOffsetY = collectionView.contentOffset.y
let width = collectionView.frame.width
let newHeight = attributes.frame.height - contentOffsetY
let height = newHeight > 64 ? newHeight: 64
attributes.frame = CGRect(x: 0, y: contentOffsetY, width: width, height: height)
if let headerView = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: attributes.indexPath) as? ProfileViewHeader {
headerView.blur(alpha: (contentOffsetY > 0) ? 0.0 : abs(contentOffsetY * 2) / 100)
}
}
return attributes
}
return nil
}