我正在尝试通过子类化UICollectionViewFlowLayout为页脚中的特定部分提供一些垂直间距。
-----------------------
Footer 0
-----------------------
vertical space from the footer = 40 ( only for the section 1)
-----------------------
Section 1
-----------------------
我正在获取间距,但是我有一个粘性页脚,该页脚未按预期显示(此偏移也会影响页脚),并且不粘到底。 p>
class OffsetFlowLayout: UICollectionViewFlowLayout {
var verticalOffset: CGFloat = 0
override func prepare() {
super.prepare()
verticalOffset = 40 // vertical spacing
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let attributes = super.layoutAttributesForItem(at: indexPath) else { return nil }
attributes.frame.origin.y += verticalOffset
return attributes
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: rect) else {
return nil
}
guard let visibleLayoutAttributes = attributes.map( { $0.copy() }) as? [UICollectionViewLayoutAttributes] else {
return nil
}
// Loop through the cache and look for items in the rect
for attributes in visibleLayoutAttributes {
if attributes.indexPath.section == 1 {
attributes.frame.origin.y += verticalOffset
}
}
return visibleLayoutAttributes
}
override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let layoutAttributes = super.layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath) else {return nil}
if layoutAttributes.representedElementKind == UICollectionElementKindSectionHeader {
layoutAttributes.frame.origin.y += verticalOffset
}
return layoutAttributes
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
在VC中
if let layout = collectionView.collectionViewLayout as? OffsetFlowLayout {
layout.sectionFootersPinToVisibleBounds = true
}