I have created a UICollectionView in which I have one prototype cell. I want 2 cells per row and running it on smaller screen gives me on one cell per row. The distance between cells gets increased on bigger screen. I am talking only with respect to iPhones.
I think I have to set constrains programmatically by taking screen width and divide it by 2. I know how to take screen width and divide it by 2 but I don't know how to give width and heights to cell programmatically.
UICollectionViewDelegateFlowLayout
defines a method for this purpose.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
// return a CGSize
}
Example:
class MyViewController : UIViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
// ...
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: CGFloat(self.view.frame.size.width / 2), height: self.view.frame.size.height)
}
// ...
}