0

I do want the view when it loads to show the right cell first then I should scroll left from there I try this code but didn't work

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! customCell
        cell.data = self.data[indexPath.row]
        self.collectionView.scrollToItem(at: IndexPath(item: self.data.count, section: 0) as IndexPath, at: .right, animated: false)
        return cell
    }

like this photo here when the view loads it shows the first right cell

enter image description here

mazenqp
  • 345
  • 4
  • 19

2 Answers2

2

Since UICollectionView scrolls horizontally from right to left equally, you can set your collection view when it appears, to appear scrolled to the maximum right ! so that the user can start scrolling from right to left

YourCollectionView is the name of your desired CollectionView

YourObjectListData is the Datasource for that collection view

self.YourCollectionView.reloadData()
self.YourCollectionView.scrollToItem(at: NSIndexPath(item: self.YourObjectListData.count - 1, section: 0) as IndexPath, at: .right, animated: false) 
Nayan Dave
  • 1,078
  • 1
  • 8
  • 30
  • thats solve the problem but why this method dont work in viewDidLoad or in any of collectionView functions ?? – mazenqp May 25 '20 at 14:07
  • Because viewDidLoad is only called once in the view lifecycle (the first time the view loads) you could add it in viewWillAppear . Try googling View life cycle methods in iOS – Ahmed Abdulkareem May 26 '20 at 07:24
-1

This is not a usual behaviour for a UICollection view, but how about rotating and flipping your collectionView, then the last cell will be your first ;).

if interested check CGAffineTransform(Rotation angle) and CGAffineTransform(TranslationY).

MOHi91
  • 65
  • 7
  • I don't know if you getting my question – mazenqp May 25 '20 at 12:50
  • Yea I though you want it to be mirrored, sorry! I did not know that you want it for a different language (Arabic) which is being read from left to right. well I guess you got the answer now :) happy coding. – MOHi91 May 26 '20 at 10:31