Reputation: 1666
i've a collectionView
with different collectionCell
and i want to set a scale/height/size depending of the cell. How can i do it ?
Here is the image of my different cell : a title cell
, and 2 different cell image
. One is width of the view / 3 and the other is / 2. I can't do it with auto layout constraint in the story board so i need to do in the code. I'm trying to use the collectionViewLayout.
Upvotes: 2
Views: 326
Reputation: 4815
As per your requirement you can do like this way
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if (indexPath.row >= 0 && indexPath.row < 3) {
return CGSize(width: self.view.frame.size.width / 3.15, height: 150)
// here give size for 3 collectionviewcell in one row . i use 3.5 beacause of i put spacing 10 pix left and right side of collectionview.
}
else if (indexPath.row >=3 && indexPath.row < 5){
return CGSize(width: self.view.frame.size.width / 2.20, height: 150)
// here give size for 2 collectionviewcell in one row.
}else{
return CGSize(width: self.view.frame.size.width / 3.15, height: 150)
}
}
Output :
Upvotes: 0
Reputation: 9503
implement
class MyViewController: UIViewController, UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewFlowLayout
in ViewDidLoad
collectionView.delegate = self;
collectionView.datasource = self;
add delegate of flow layout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2{
return CGSize(self.view.frame.size.width / 3, 100)
}
else{
return CGSize(self.view.frame.size.width / 2, 100)
}
// return the size you what
}
Upvotes: 1