Note: Below answer based on the discussion between question owner and myself.
You need to setup a collectionViewFlowLayout
properly to achieve the desired output. Try below code:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
// All the values are changable according to your needs.
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets.zero
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 2
return CGSize(width: (self.collectionView.frame.width / 2) - 1 , height:(self.collectionView.frame.height / 3) - 1)
}
Update 1: Calculating layout includes navigationBar
and StatusBar
.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
// All the values are changable according to your needs.
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets.zero
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 2
return CGSize(width: (self.collectionView.frame.width / 2) - 1 , height:(((self.view.frame.height - ((navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height)) / 3) - 1))
}
You may need to validate collectionView frame in viewDidLayoutSubviews
or viewWillLayoutSubviews
override func viewDidLayoutSubviews() {
collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}
Following link may help you to understand the collectionViewFlowLayout
.
Enforce collectionView to have only 2 rows
uicollectionview remove top padding
Adjust cellsize for fit all screens?
Note: In future, Post your question with relevant code and state problem clearly.