Question

I am using a UICollectionView with fixed size CollectionViewCells. I display this CollectionView in a popOver with an UIPopoverController.

My goal is that the popOver automatically resize to the CollectionView's content. So if I only have 4 cells, than it should be small. And if I have more cells, it should be bigger. I want to avoid the empty space left in the popOver.

So I don't want a fixed popOver size, but a variable one depending on the number of cells in the CollectionView.

Any suggestions?

Was it helpful?

Solution

I had a hard time figuring this out myself but just got it finally. You have to access your Collection View's collectionViewLayout property and then use the collectionViewContentSize method to get the size of your content. You can then use those to resize your Collection View by setting it's frame.

CGRect rectangle;
rectangle = _collectionView.frame;
rectangle.size = [_collectionView.collectionViewLayout collectionViewContentSize];
_collectionView.frame = rectangle;

Now that your collection view is sized to it's content, you can set the popOver's size based on the collection view size. It may look something like this.

popOver.frame = _collectionView.frame;

or

[popOver sizeToFit];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top