title: UICollectionView vertical flow layout keep in collection bounds
body: <p>I have a UICollectionView with vertical cells alignment, but I want also vertical section alignment, I also want to keep the sections inside the current frame of the UICollectionView, that means if a section reach the end and it's longer then the collection itself, then it will go to next line.</p>
  
  <p>This is how it looks right now:</p>
  
  <p><a href="https://i.stack.imgur.com/0WWp9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0WWp9.png" alt="collectionview"></a></p>
  
  <p>This is the final result I want to end up with:</p>
  
  <p><a href="https://i.stack.imgur.com/2lqWS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2lqWS.png" alt="final collection"></a></p>
  
  <p>This is my code so far:</p>
  
  <pre><code>class LettersCollectionTestViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  
      @IBOutlet weak var collectionContainer: UIView!
      var collectionView: UICollectionView?
  
      private let ReuseIdentifierCollectionLetterCell = "LetterCollectionViewCell"
  
      var collectionWords: NSArray?
  
      var itemSize: CGFloat = 25.0
      var itemSpacing: CGFloat = 5.0
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Do any additional setup after loading the view.
  
          let logoWord: String = "This is just this"
          self.collectionWords = logoWord.componentsSeparatedByString(" ")
          self.initializeCollection()
      }
  
      func initializeCollection() {
  
          let collectionViewLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
          collectionViewLayout.itemSize = CGSizeMake(self.itemSize, self.itemSize)
          collectionViewLayout.minimumInteritemSpacing = 0
          collectionViewLayout.minimumLineSpacing = self.itemSpacing
          collectionViewLayout.scrollDirection = UICollectionViewScrollDirection.Vertical
  
          let collectionView: UICollectionView = UICollectionView(frame: self.collectionContainer.bounds, collectionViewLayout: collectionViewLayout)
          collectionView.delegate = self;
          collectionView.dataSource = self;
          collectionView.showsHorizontalScrollIndicator = false
          collectionView.showsVerticalScrollIndicator = false
  
          collectionView.registerClass(LetterCollectionViewCell.self, forCellWithReuseIdentifier: self.ReuseIdentifierCollectionLetterCell)
          collectionView.registerNib(UINib(nibName: self.ReuseIdentifierCollectionLetterCell, bundle: nil), forCellWithReuseIdentifier: self.ReuseIdentifierCollectionLetterCell)
  
          self.collectionView = collectionView
          self.collectionContainer.addSubview(collectionView)
      }
  
      // MARK: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  
      func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
          print("clicked item at index: \(indexPath.row)")
      }
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
          let cell: LetterCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.ReuseIdentifierCollectionLetterCell, forIndexPath: indexPath) as! LetterCollectionViewCell
  
          let scalingTransform: CGAffineTransform = CGAffineTransformMakeScale(-1, 1)
          cell.transform = scalingTransform
  
          return cell
      }
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return (self.collectionWords?.objectAtIndex(section).length)!
      }
  
      func numberOfSectionsInCollectionView(collectionView: UICollectionView) -&gt; Int {
          return (self.collectionWords?.count)!
      }
  
      func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -&gt; UIEdgeInsets {
          return UIEdgeInsets(top: self.itemSpacing, left: self.itemSpacing, bottom: self.itemSpacing, right: self.itemSpacing)
      }
  
  }
  </code></pre>
  
  <p>What do I need to do? I know I should create a custom FlowLayout, but where should I start? I never did it before.</p>
  
  <p>Thanks in advance!</p>