Adapt size of items layout in CollectionView according to screensize maintaining the aspect ratio [Swift]
<p>I'm trying to adapt the size of items layout in a CollectionView to the screensize maintaining the aspect ratio.  Till now it works well only on iPad, I've setted an height and a width, according to the dimensions of the assets:</p>
<pre><code>private let CardWidth: CGFloat = 362
private let CardHeight: CGFloat = 568
</code></pre>
<p>Then in <code>CardLayout</code> class, which inherits from <code>UICollectionViewFlowDelegate</code>:</p>
<pre><code>required init?(coder aDecoder: NSCoder) {
   super.init(coder: aDecoder)
    
   scrollDirection = UICollectionViewScrollDirection.Horizontal
   itemSize = CGSizeMake(PageWidth, PageHeight)
   minimumInteritemSpacing = 10
}
</code></pre>
<p>Problem is that items are cards, so <code>height</code> &gt; <code>width</code>, while i'm developing for both iPhone and iPad in landscape only, so i've tried to use <code>UIScreen.mainScreen().bounds</code> without success. I managed to get a decent result for both iPhone and iPad adding in <code>CollectionViewController</code>:</p>
<pre><code>func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -&gt; CGSize{

retrun CGSizeMake(collectionView.frame.size.width / 2 ,collectionView.frame.size.height/ 2)
}
</code></pre>
<p>where i swapped <code>width</code> and <code>height</code> in <code>return</code>, since the app is landscape only. Still this is not the best solution, since cards does not maintain the aspect ratio. So is there a way to adapt size to screen maintaining the aspect ratio?
Help is appreciated.</p>