Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display cells of UICollectionView from right to left instead of left to right?

This is what I have now (by default):

enter image description here

but I would like to have:

enter image description here

I display it using NSFetcheResultsController with descending order for my property.

like image 834
Bartłomiej Semańczyk Avatar asked Dec 18 '22 12:12

Bartłomiej Semańczyk


1 Answers

I faced the same problem, after searching for any possible built-in feature that let me able to do this easily, -unfortunately- I couldn't find such a thing. I had to solve it by my self by doing this trick:

For the purpose of demonstration, I will present this -ugly- collectionView and show you how it should changes:

enter image description here

The main idea of the trick is Mirroring! by changing the scaleX to -1.

Let's mirror the whole collection view:

P.S: Swift 3 code.

If you don't have an IBOutlet for your collectionView, create one. In my case im calling it collectionView

In viewController:

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.transform = CGAffineTransform(scaleX: -1, y: 1)
}

Now, it should look like this:

enter image description here

"What the heck?!"

So far so good, in fact this is exactly what you are looking for, but you still need to add another mirroiring mechanism for each cell content.

In your custom cell's class:

override func awakeFromNib() {
    super.awakeFromNib()

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

Now it should look like this:

enter image description here

Hope it helped.

like image 169
Ahmad F Avatar answered May 10 '23 14:05

Ahmad F