add cells in UICollectionView from bottom to top using Swift 4.2

I'm trying to implement a Chat screen using UICollectionView in Swift 4.2. I've done it. I want to improve it by making the cells grow from bottom to top instead of top to bottom (while keeping the order of messages too). Any help? I've tried searching for it, still unsuccessful.

enter image description here

2 answers

  • answered 2018-11-08 07:21 Lok SN

    The easiest way would be to flip the collection view and its cells:-

    cv.transform = CGAffineTransform(scaleX: 1, y: -1)
    cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1)
    

    Doing this will just flip your CollectionView's content and won't require you to handle anything, I guess

    EDIT:-

    For your requirement, you shouldn't be appending elements to your array. You should insert new objects as the first element of the array(the data source):-

    myArray.insert(element, at: 0)
    

    In order to get the right order, you can just reverse the array

  • answered 2018-11-08 18:50 Mohammad Tahir

    You have to make collection view height constraint outlet for it and calculate your cells height after that you can set height of collectionView. and one case will come of maximum height, your collection height will equal from screen height with calculate space your navigation and input textview.