I tried to give the height and width for collectionViewCell
, programatically. I used following method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let viewWidth = self.deviceWidth/3 - 5
let viewHeight = viewWidth
return CGSize(width :100,height: 100)
}
This didn't work. even this method didn't show as a hint. Help me with this. Is there any other delegate method to set the cell width and height programmatically in Swift 3?
Method
collectionView(_:layout:sizeForItemAt:)
isUICollectionViewDelegateFlowLayout
protocol's method, so settingdelegate
anddatasource
ofcollectionView
is not enough, you need to implementUICollectionViewDelegateFlowLayout
with your custom class where you have addedcollectionView
to callUICollectionViewDelegateFlowLayout
protocol's methods. So implementUICollectionViewDelegateFlowLayout
like below example:class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
Pls sure your class must implement UICollectionViewDelegateFlowLayout like this:
And then write this delegate method in your class:
Hope it will help you...
This methods always work. Please first ensure that this method is executed ot not. If not, the write delegate
UICollectionViewDelegateFlowLayout
like this:And dont forget to bind the delegate and datasource of UICollectionView either from storyboard or by code
collectionView.delegate = self
andcollectionView.datasource = self