title: How to align UICollectionViewCell?
body: <p>I am trying to build a <code>UICollectionView</code>as tag flow layout according to this topic: <a href="http://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/" rel="nofollow noreferrer">http://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/</a></p>
  
  <p>It worked until the button action but there is one issue. As you can see at the picture below, custom <code>UICollectionViewCell</code>'s don't align correctly as like as at the topic. I did everything same but <code>FlowLayout.swift</code></p>
  
  <p><a href="https://i.stack.imgur.com/Ry0PV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ry0PV.png" alt="enter image description here"></a></p>
  
  <p>I want to make equal gaps between these cells. But they are aligning like this.</p>
  
  <p>You can find <code>FlowLayout.swift</code> below. </p>
  
  <p>Can someone tell me how can I fix it? </p>
  
  <pre><code>import Foundation
  import UIKit
  
  class FlowLayout: UICollectionViewFlowLayout {
  
      override func layoutAttributesForElementsInRect(rect: CGRect) -&gt; [UICollectionViewLayoutAttributes]? {
  
          let attributesForElementsInRect = super.layoutAttributesForElementsInRect(rect)
          var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
  
          var leftMargin : CGFloat = 0.0
  
          for attributes in attributesForElementsInRect! {
              let refAttributes = attributes
              // assign value if next row
              if (refAttributes.frame.origin.x == self.sectionInset.left) {
                  leftMargin = self.sectionInset.left
              } else {
                  // set x position of attributes to current margin
                  var newLeftAlignedFrame = refAttributes.frame
                  newLeftAlignedFrame.origin.x = leftMargin
                  refAttributes.frame = newLeftAlignedFrame
              }
              // calculate new value for current Fmargin
              leftMargin += refAttributes.frame.size.width + 8
              newAttributesForElementsInRect.append(refAttributes)
          }
  
          return newAttributesForElementsInRect
      }
  
  
  }
  </code></pre>