func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == self.collectionViewVideo {
var collectionViewSize = collectionViewVideo.frame.size
collectionViewSize.width = collectionViewSize.width/3.0 //Display Three elements in a row.
return collectionViewSize
} else {
return CGSize(width: 60, height: 60)
}
}
Asked
Active
Viewed 3,112 times
0

Community
- 1
- 1

Gorib Developer
- 587
- 2
- 13
- 27
1 Answers
5
You also need to take space in account. Space is your collection view item space
Following is the code has two cells and 20 spaces. (I am not writing exactly code you need You should do it by your self)
extension YourViewController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 20
}
//--------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 20;
}
//--------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: ( self.collectionView.frame.size.width - 60 ) / 2,height:( self.collectionView.frame.size.width - 60 ) / 2)
}
//--------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 20, 20, 20)
}
}
Here 60
means SPACE 20 CELL SPACE 20 CELL SPACE 20

Prashant Tukadiya
- 15,838
- 4
- 62
- 98
-
what is insetForSectionAt here sir? – Gorib Developer Apr 12 '18 at 12:05
-
inset is position where your cell will start. With that you can apply left space to your first cell (20 px) and right space to your right side cell (20 px) – Prashant Tukadiya Apr 12 '18 at 12:07
-
@GoribDeveloper Try to play with that part and try to understand it. So you don't need next time to put question or need help from internet – Prashant Tukadiya Apr 12 '18 at 12:08
-
@GoribDeveloper If my answer if helpful if you would not mind could you please mark it as accepted :) – Prashant Tukadiya Apr 12 '18 at 12:19