Curnelious Curnelious - 5 months ago 63
Swift Question

Can not change collection view content size dynamically?

I am trying to change a collection view content size with :

collectionView.contentSize.height = 1.25*collectionView.contentSize.height

(or any variation of this line of code)

Since we are adding views at the bottom of its content, after the last cell.

This line of code will not work, and the bottom views are out of screen, you can scroll only to the last cell (default height)

I have tried to read any answer here, non was specific/solve the problem.

How can one simply change the content height of a collection ?


The content size of your collection view is calculated by its 'collectionViewLayout'. If you want to change the content size then you'll have to subclass a collection view layout, and override "collectionViewContentSize", and return whatever size you want. It would probably look something like this (assuming you are using a flow layout):

- (CGSize)collectionViewContentSize {
    CGSize size = [super collectionViewContentSize];
    size.height += yourCustomView.frame.size.height;
    return size;