0

I have a side-scrolling collection view that contains two images. These images may be changed by the user during runtime. This change might cause a change of the collection view cell size, hence the collection view also need to adapt its height according to the height of the cells.

I've implemented the UICollectionViewDelegateFlowLayout protocol and the sizeForItemAt function which returns the proper cell size given the image.

I also have a height constraint outlet connected to my collection view which allows me to change its height, matching the height value of the chosen image.

It is all presented as you would expect when drawn to the screen, but I get the error "The behavior of the UICollectionViewFlowLayout is not defined because..", complaining that the height returned by sizeForItemAt exceeds my collection views default height (set in the storyboard).

It seems that I need to invalidate my collection view layout once I've altered its height constraint so that the collection view layout is updated with this new value before sizeForItemAt returns the cell sizes.

I've tried to call UICollectionViewLayouts invalidateLayout() right after updating the collection view height constraint, but that didn't help.

Any ideas to what I am doing wrong?

jalpa jani
  • 27
  • 4
PistolPete
  • 766
  • 3
  • 10
  • 25

1 Answers1

0

SOLVED

Ok, what I needed to do was to call view.layoutIfNeeded() for my view controller that holds the collection view right after I changed the collection views height constraint. Then the bounds for the collection view will be updated with the new height value so that the cells will fit.

PistolPete
  • 766
  • 3
  • 10
  • 25