title: Custom collection flow layout for UICollectionView in Swift
body: <p>i'm learning iOS with Swift, and now i wanted to create simple messenger app (very very simple version of WhatsApp). I've looked through all collection view video tutorials, but i still cannot understand how to create a layout, where cells will be on two sides of the screen (two cells couldn't be in one row). 
  So as far as i understand, i should go with flow layout. I override layoutAttributesForElementsInRect like this:</p>
  
  <pre><code>override func layoutAttributesForElementsInRect(rect: CGRect) -&gt; [AnyObject]? {
      let attributes = super.layoutAttributesForElementsInRect(rect) as! [UICollectionViewLayoutAttributes]
      for attribute in attributes {
          attribute.frame.origin.x = collectionView!.bounds.width - attribute.frame.width
      }
      return attributes
  }
  </code></pre>
  
  <p>And now my cells lay under each other (for example i have 50 cells, but i could see only 10 with 5 cells in depth). </p>
  
  <p>EDIT: As you can see, 5 cells lay under each other in each row, so totally i got 10 rows, not 50.</p>
  
  <p><a href="https://i.stack.imgur.com/NZeAC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NZeAC.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/9ecBz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9ecBz.png" alt="enter image description here"></a></p>
  
  <p>And i want to see all 50 cells from right side.</p>
  
  <p>So could somebody give any advice how can i implement this layout. 
  Thanks for any help!</p>