Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open class Layout_TableAuto : UICollectionViewFlowLayout
- {
- // Don't forget to use this class in your storyboard (or code, .xib etc)
- // Try to position the supplementary views below the last item of the previous section
- open override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
- guard let attributes = super.layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath) else { return nil }
- guard let collectionView = collectionView else { return attributes }
- // Last index
- let section = indexPath.section
- let prevSection = section-1
- if section == 0 {
- return attributes
- }
- let lastItemIndex = collectionView.numberOfItems(inSection: prevSection)-1
- var newY : CGFloat = attributes.frame.origin.y
- print("Section: \(indexPath.section)")
- print("AttributeFrame: \(attributes.frame)")
- if let itemAttributes = layoutAttributesForItem(at: IndexPath(item: lastItemIndex, section: prevSection)){
- print("ItemAttributes: \(itemAttributes.frame)")
- //attributes?.frame.origin.y = itemAttributes.frame.origin.y + itemAttributes.frame.size.height
- newY = itemAttributes.frame.origin.y + itemAttributes.frame.size.height
- }
- // On the final pass, this value is correct, but the supplementary view does not update its position
- print("newY: \(newY)")
- attributes.frame.origin.y = newY
- print("New Attributes: \(attributes.frame)")
- return attributes
- }
- override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
- // let attributes = super.layoutAttributesForItem(at: indexPath)?.copy() as? UICollectionViewLayoutAttributes
- let attributes = super.layoutAttributesForItem(at: indexPath)
- guard let collectionView = collectionView else { return attributes }
- attributes?.bounds.size.width = collectionView.bounds.width - sectionInset.left - sectionInset.right
- return attributes
- }
- override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
- print("layoutAttributesForElements: \(rect)")
- let allAttributes = super.layoutAttributesForElements(in: rect)
- return allAttributes?.flatMap { attributes in
- switch attributes.representedElementCategory {
- case .cell: print("cell: \(attributes.indexPath)"); return layoutAttributesForItem(at: attributes.indexPath)
- case .supplementaryView: print("supp: \(attributes.indexPath)"); return layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: attributes.indexPath)
- default: return attributes
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement