1

I just can´t get familar with auto layout in Xcode. When I set trailing and leading constrains for Header Stack View, Bottom Stack View or the image between them, then the size (height/width) of my cell changes. Why does it happen and how can I avoid it? I think something isn´t right with the kind I build layouts. I am very grateful for any help.

I set the cell size with this code:

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

        let height = view.frame.size.height
        let width = view.frame.size.width

        return CGSize(width: width * 0.42, height: height * 0.3)
    }

}

My cell layout:

enter image description here

This is how the app looks when I use constrains for the Bottom Stack View.

enter image description here

This is without constrains for the Bottom Stack View (correct cell size).

enter image description here

Kinuhatek
  • 85
  • 2
  • 10

1 Answers1

2

Cells can now self-size with auto layout and ignore what you return in collectionView(_:layout:sizeForItemAt:). If you want to use collectionView(_:layout:sizeForItemAt:), change Estimate size to None in the storyboard. https://stackoverflow.com/a/58369142/14351818

aheze
  • 24,434
  • 8
  • 68
  • 125