Oluwatobi Omotayo
Oluwatobi Omotayo

Reputation: 1879

How to make UICollectionView new cells load from bottom in Swift

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?

Upvotes: 3

Views: 3883

Answers (2)

Hocine B
Hocine B

Reputation: 389

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

Upvotes: 0

iVarun
iVarun

Reputation: 6611

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)

Upvotes: 7

Related Questions