krosh
krosh

Reputation: 218

dynamic change of UICollectionViewCell width

I have a View with an imageView and collectionView. ImageView is in the left top corner of screen with size (150,250).CollectionView is full-screen. Cell of collectionView has label inside it.

When the cell is near the imageView, it should have smaller width. When the cell is lower then imageView, it should be on full width. This should also work, while scrolling, so cell's size should change dynamically. So, I need to make a custom layout. I thought, that "layoutAttributesForElementsInRect:" is an answer. But how to make cell attributes change in certain rectangle? Or am I looking to the wrong direction? Thank you in advance.

example

Upvotes: 2

Views: 5741

Answers (1)

Jay Gajjar
Jay Gajjar

Reputation: 2741

I think your are looking for the invalidateLayout method you can call on the collectionViewLayout property of your UICollectionView. This method regenerates your layout, which in your case means also calling-collectionView: layout: sizeForItemAtIndexPath:, which is the right place to reflect your desired item size. Jirune points the right direction on how to calculate them.

An example for the usage of invalidateLayout can be found here. Also consult the UICollectionViewLayout documentation on that method:

Invalidates the current layout and triggers a layout update.

Discussion:

You can call this method at any time to update the layout information. This method invalidates the layout of the collection view itself and returns right away. Thus, you can call this method multiple times from the same block of code without triggering multiple layout updates. The actual layout update occurs during the next view layout update cycle.

Upvotes: 5

Related Questions