Search code examples
iosswiftuicollectionviewcell

Self sized UICollectionViewCell with fixed Height/Width


So I have this collection view with mixed self sized cells and fixed sized cells. It works just fine for self the self sizing ones but I can't get the fixed ones to stay fixed sized. Here is what I do :

// I have a viewContainer inside the cell's contentView
let viewContainer = UIView()
viewContainer.backgroundColor = .red
viewContainer.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(viewContainer)

// This viewContainer has a fixed width/height and is achored to the top, left, right, bottom of the contentView
viewContainer.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
viewContainer.leftAnchor.constraint(equalTo: self.contentView.leftAnchor).isActive = true
viewContainer.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
viewContainer.rightAnchor.constraint(equalTo: self.contentView.rightAnchor).isActive = true
viewContainer.heightAnchor.constraint(equalToConstant: FIXED_HEIGHT).isActive = true
viewContainer.widthAnchor.constraint(equalToConstant: FIXED_WIDTH).isActive = true

// Then I got my ImageView (property of my cell), as a viewContainer subview
self.photoImageView.translatesAutoresizingMaskIntoConstraints = false
self.photoImageView.contentMode = .scaleAspectFill
viewContainer.addSubview(self.photoImageView)

// It has a fixed width/height and is achored to the top, left, right, bottom of the viewContainer
self.photoImageView.topAnchor.constraint(equalTo: viewContainer.topAnchor).isActive = true
self.photoImageView.leftAnchor.constraint(equalTo: viewContainer.leftAnchor).isActive = true
self.photoImageView.bottomAnchor.constraint(equalTo: viewContainer.bottomAnchor).isActive = true
self.photoImageView.rightAnchor.constraint(equalTo: viewContainer.rightAnchor).isActive = true
self.photoImageView.heightAnchor.constraint(equalToConstant: FIXED_HEIGHT).isActive = true
self.photoImageView.widthAnchor.constraint(equalToConstant: FIXED_WIDTH).isActive = true

Somehow, when I had images (from an image picker, with a simple reload data in the delegate), the cell is resized, the size of the image :

unwanted self sized cell

But after a quick scroll up and down on the collectionView, it looks fine :

right sized cell

I tired to add a high priority on the heightAnchor and withAnchor UILayoutPriority(999.0) but it doesn't seem to help. I can't find a way to size that cell and the cell's image how I want. I feel like it's related to the image inside the image view but I don't understand how and why it does that. Can anyone help me with this ?

Thank you in advance for your answers.

EDIT : I also tried to create a UIImageView subclass and override the intrinsicContentSize to make it return the size I wanted it to be, but still no success :/


Solution

  • Not sure it's the best solution but I ended adding a width/height constraints on the cell's contentView with 999 priority. I still have Autolayout warnings. But it seems to work "ok".