Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.92 KB | None | 0 0
  1. open class Layout_TableAuto : UICollectionViewFlowLayout
  2. {
  3.     // Don't forget to use this class in your storyboard (or code, .xib etc)
  4.    
  5.     // Try to position the supplementary views below the last item of the previous section
  6.     open override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
  7.        
  8.         guard let attributes = super.layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath) else { return nil }
  9.         guard let collectionView = collectionView else { return attributes }
  10.        
  11.         // Last index
  12.         let section = indexPath.section
  13.         let prevSection = section-1
  14.         if section == 0 {
  15.             return attributes
  16.         }
  17.         let lastItemIndex = collectionView.numberOfItems(inSection: prevSection)-1
  18.        
  19.         var newY : CGFloat = attributes.frame.origin.y
  20.         print("Section: \(indexPath.section)")
  21.         print("AttributeFrame: \(attributes.frame)")
  22.        
  23.         if let itemAttributes = layoutAttributesForItem(at: IndexPath(item: lastItemIndex, section: prevSection)){
  24.             print("ItemAttributes: \(itemAttributes.frame)")
  25.             //attributes?.frame.origin.y = itemAttributes.frame.origin.y + itemAttributes.frame.size.height
  26.             newY = itemAttributes.frame.origin.y + itemAttributes.frame.size.height
  27.         }
  28.        
  29.         // On the final pass, this value is correct, but the supplementary view does not update its position
  30.         print("newY: \(newY)")
  31.         attributes.frame.origin.y = newY
  32.         print("New Attributes: \(attributes.frame)")
  33.         return attributes
  34.     }
  35.    
  36.     override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
  37. //        let attributes = super.layoutAttributesForItem(at: indexPath)?.copy() as? UICollectionViewLayoutAttributes
  38.         let attributes = super.layoutAttributesForItem(at: indexPath)
  39.         guard let collectionView = collectionView else { return attributes }
  40.         attributes?.bounds.size.width = collectionView.bounds.width - sectionInset.left - sectionInset.right
  41.         return attributes
  42.     }
  43.    
  44.     override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  45.         print("layoutAttributesForElements: \(rect)")
  46.        
  47.         let allAttributes = super.layoutAttributesForElements(in: rect)
  48.         return allAttributes?.flatMap { attributes in
  49.             switch attributes.representedElementCategory {
  50.             case .cell: print("cell: \(attributes.indexPath)"); return layoutAttributesForItem(at: attributes.indexPath)
  51.             case .supplementaryView: print("supp: \(attributes.indexPath)"); return layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: attributes.indexPath)
  52.             default: return attributes
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement