Philip007
Philip007

Reputation: 3230

Set UICollectionViewFlowLayout object attributes directly?

Is it possible to set UICollectionViewFlowLayout object attributes directly without touching UICollectionViewDelegateFlowLayout protocol?

For example, if I want every item size to be the same, could I just do this:

self.collectionViewFlowLayout.itemSize = CGSizeMake(100,100)

instead of this:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 100);
}

If positive, how can I reference the UICollectionViewFlowLayout object?

If negative, could you explain why Apple choose to make it a protocol? It obviously requires more code.

Upvotes: 0

Views: 981

Answers (2)

Teo Sartori
Teo Sartori

Reputation: 1112

You can use the default flow layout from your UICollectionViewController. For instance, from your viewDidLoad you can call

UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)[[self collectionView] collectionViewLayout];

and then set the the attributes with:

[flowLayout setItemSize:CGSizeMake(100.0,100.0)];

Upvotes: 3

Moshe
Moshe

Reputation: 58097

Yes, it is possible, but not by accessing an existing UICollectionViewFlowLayout instance. You need to subclass UICollectionViewFlowLayout and handle all of the attributes that you'd like to in your subclass.

There were some great WWDC sessions on that this year, and I recommend watching them. You can see Session 205 here (login required) and Session 219, here(login required). The second video will address your question directly.

Upvotes: 1

Related Questions