1

when i implement below code :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{

    let cellW = (ScreenWidth - 10 - 10 - 30) / 4

    return CGSize(width: cellW, height: cellW * 1.5 )

}

by using sizeForItemAt method , i can draw frame what i want , but my custom collectionview cell can not update as wise , where did i miss , plz help me !!

my custom cell

debug view

黃翔煜
  • 63
  • 5
  • Add constraint for your image to leading/trailing/top to superView? And add missing constraints for the bottom view? – Larme Jun 12 '17 at 14:03
  • i aready did add constraint for my imageview and label , it's seem collectionview cell's problem , maybe i missing something in awakefromNib? http://imgur.com/a/6N9qC – 黃翔煜 Jun 12 '17 at 14:09
  • http://imgur.com/a/XOVTn this is my storyboard collectionview cell frame – 黃翔煜 Jun 12 '17 at 14:11
  • Did you write that you implement UICollectionViewDelegateFlowLayout in you class declaration? – iamirzhan Jun 12 '17 at 14:14
  • yes i did implement UICollectionViewDelegateFlowLayout delegate , so that i can draw the area (see in debug view : https://imgur.com/a/TyjOr) – 黃翔煜 Jun 12 '17 at 14:20

2 Answers2

1

i fix it by this ref UICollectionView cell subviews do not resize

thanks guys , at frist still have problem with my Cell.imageview , can not shape-round as i want , and i create a class for imageview ,below:

class CellImageView: UIImageView {
override func layoutSubviews() {
    super.layoutSubviews()
    let radius: CGFloat = self.bounds.size.width / 2.0
    self.layer.cornerRadius = radius
    self.clipsToBounds = true

}

and anything work perfectly

黃翔煜
  • 63
  • 5
0

For me in iOS 13, I have fixed by first estimatedItemSize to .zero and set the itemSize like;

if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
 layout.estimatedItemSize = .zero
 layout.itemSize = CGSize(width: collectionView.frame.size.width, height: 80)
}
alitosuner
  • 984
  • 1
  • 10
  • 15