3

I have a collectionview to display a list of comments, but I want the cells to start displaying from bottom, a pattern seen in the WhatsApp and Telegram App for iOS (new messages starts from the bottom and populate upwards), like in the image below. Telegram iOS Screenshot

I was searching online but couldn't find something that worked. I tried this code below from one answer I saw on SO but it didn't work as well.

let contentSize = collectionView.collectionViewLayout.collectionViewContentSize
    if contentSize.height > collectionView.bounds.size.height {
        collectionView.contentOffset = CGPoint(x: 0, y: contentSize.height - collectionView.bounds.size.height)
    }

How do I achieve this?

Oluwatobi Omotayo
  • 1,719
  • 14
  • 28
  • You can do buy adding empty cell with calculated height as the first cell. Or for a wild idea, transform table (rotate) 180 degree and transform each cells (180 degree) just before presenting (tableView(_:willDisplay:forRowAt:) ). I actually did this long time ago for some smaller project. :) – RJE Jan 15 '18 at 12:58
  • did you checked my answer? is it worked for you? – iVarun Jan 15 '18 at 14:25

2 Answers2

7

You need to transform your collection and it's cell. Use below code to rotate your collection view:

yourCollectionView.transform = CGAffineTransform.init(rotationAngle: (-(CGFloat)(Double.pi)))

For cell use below code:

cell.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
iVarun
  • 6,496
  • 2
  • 26
  • 34
0

For those who don't want to use graphics workaround to fix what should be a matter of model, just override layoutAttributesForItem function in a custom UICollectionViewFlowLayout

Hocine B
  • 391
  • 2
  • 8