What is the way to set the equal size height for all cells in collection view?
I have this method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return getCellSize()
}
func getCellSize() -> CGSize {
let screenWidth: CGFloat = UIScreen.main.bounds.width
let spaceBetweenItems: CGFloat = CGFloat(2) * 10
let width: CGFloat = (screenWidth - 10 - spaceBetweenItems) / CGFloat(3.0)
return CGSize(width: width, height: CGFloat(140.0))
}
And these are my customs cell constraints:
What is wrong in my code or is missing one constraint ?
2
Answers
In storyboard with collection view: select your collection view -> go to size inspector -> change field "Estimate size" from "automatic" to "none"
like here.
But then if you text will be too large it will be clipped with dots like "Some large text..", so you can change field "Autoshrink" of your UILabel in attribute inspector to "Minimum Font Scale".
create Constraint CollectionViewHeight.
and then, try this Code.