title: Dynamic UICollectionViewCell width with respect to frame width and aspect ratio
body: <p>This is a follow up question from <a href="https://stackoverflow.com/questions/26905191/uicollectionviewcell-dynamic-sizing-for-4-4-and-5-5-inch/26906328?noredirect=1#comment60216926_26906328">UICollectionViewCell dynamic sizing for 4.4 and 5.5 inch</a> </p>
  
  <p>While that solution is great, I'd like to generate a dynamic amount of item cells (kCellsPerRow) within the UIVIewController based on the device's width, aspect ratio, and amount of total items I have to display. I just can't figure out how to calculate the right amount of kCellsPerRow so that they are not too resized once they hit an aspect ratio limit.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>var kCellsPerRow = ceil(global.TotalAmount)                //Total Amount of Cells
  var windowWidth = layout.collectionView!.frame.width        // Device's Window Width 
  var dynamicWidth = windowWidth / CGFloat(kCellsPerRow)    // Current Width of Each Cell
  var dynamicHeight = windowWidth / CGFloat(kCellsPerRow)    // Current Height of Each Cell
  var limit = (Double(dynamicWidth) / Double(windowWidth))*(9/2) &lt; (2/9)    // Constraint/Threshold
  
  if ( limit ) {
      var newkCellsPerRow = CGFloat(kCellsPerRow * (2/9))    // Attempt
      newkCellsPerRow = 9  // Fixed Size of 9 but what if we can dynamically figure out the perfect number such that its not so tiny!
  
      dynamicWidth = layout.collectionView!.frame.width / newkCellsPerRow
      dynamicHeight = layout.collectionView!.frame.width / newkCellsPerRow
  }
  
  // Create Cell
  layout.itemSize = CGSize(width: dynamicWidth, height: dynamicHeight)
  </code></pre>