1

I am trying to mimic Instagram live comments section where the first cell in the UICollectionView appears at the bottom but the flow is still top to bottom. I.e. everything about the flow layout is the exact same except that the first cell is at position (x: 0, y: heightOfCollectionView)...

Below is my naive implementation. I also thought about having the collection view increment in height per item in the UICollectionView until themaxY >= centerY of the superview.

import UIKit

class CommentsLayout: UICollectionViewFlowLayout {

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let layoutAttribute = super.layoutAttributesForItem(at: indexPath)?.copy() as! UICollectionViewLayoutAttributes
    if indexPath.section == 1 {
        print(collectionView?.frame.size.height)
        layoutAttribute.frame = CGRect(x: 0, y: 600, width: collectionViewContentSize.width, height: 40)
    }
    return layoutAttribute
}

}

a.masri
  • 2,439
  • 1
  • 14
  • 32
jerome
  • 31
  • 3
  • Why don't you just reverse your data model collection? – Vadim Popov May 06 '18 at 17:39
  • 1
    @VadimPopov I want the very first cell to be inserted at the bottom and all cells after that to be inserted at the bottom -- UICollectionViewFlowLayout has it such that if you have a UICollectionView with some height (600) your items will display at the top (origin). Reversing the data model will actually create the opposite effect when you get towards the bottom. – jerome May 06 '18 at 23:55
  • @Jerome did you find any solution to this problem? – StackGU Mar 13 '21 at 15:03

0 Answers0