From 5eb75d6bc79390c9b2f650bd6e40b68e92394185 Mon Sep 17 00:00:00 2001 From: Levi McCallum Date: Mon, 3 Aug 2015 14:36:29 -0700 Subject: [PATCH] Add support for sectionInset in ASCollectionView in property and delegate form --- AsyncDisplayKit/ASCollectionView.h | 13 +++++++++++++ AsyncDisplayKit/ASCollectionView.mm | 12 ++++++++++-- examples/ASCollectionView/Sample/ViewController.m | 4 ++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/AsyncDisplayKit/ASCollectionView.h b/AsyncDisplayKit/ASCollectionView.h index a47765bbfa..7af28f96e3 100644 --- a/AsyncDisplayKit/ASCollectionView.h +++ b/AsyncDisplayKit/ASCollectionView.h @@ -320,6 +320,19 @@ */ - (BOOL)shouldBatchFetchForCollectionView:(ASCollectionView *)collectionView; +/** + * Passthrough support to UICollectionViewDelegateFlowLayout sectionInset behavior. + * + * @param collectionView The sender. + * @param collectionViewLayout The layout object requesting the information. + * #param section The index number of the section whose insets are needed. + * + * @discussion The same rules apply as the UICollectionView implementation, but this can also be used without a UICollectionViewFlowLayout. + * https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegateFlowLayout_protocol/index.html#//apple_ref/occ/intfm/UICollectionViewDelegateFlowLayout/collectionView:layout:insetForSectionAtIndex: + * + */ +- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section; + @end @interface ASCollectionView (Deprecated) diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index e13c0d5167..b5b132dca8 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -118,6 +118,7 @@ static BOOL _isInterceptedSelector(SEL sel) NSMutableArray *_batchUpdateBlocks; BOOL _asyncDataFetchingEnabled; + BOOL _implementsInsetSection; ASBatchContext *_batchContext; } @@ -164,6 +165,8 @@ static BOOL _isInterceptedSelector(SEL sel) _performingBatchUpdates = NO; _batchUpdateBlocks = [NSMutableArray array]; + + _implementsInsetSection = ([_asyncDelegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)] ? 1 : 0); [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"_ASCollectionViewCell"]; @@ -511,11 +514,16 @@ static BOOL _isInterceptedSelector(SEL sel) - (CGSize)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath { CGSize restrainedSize = self.bounds.size; + UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionViewLayout sectionInset]; + + if (_implementsInsetSection) { + sectionInset = [_asyncDelegate collectionView:self layout:self.collectionViewLayout insetForSectionAtIndex:indexPath.section]; + } if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) { - restrainedSize.width = FLT_MAX; + restrainedSize.width = MAX(0, FLT_MAX - sectionInset.left - sectionInset.right); } else { - restrainedSize.height = FLT_MAX; + restrainedSize.height = MAX(0, FLT_MAX - sectionInset.top - sectionInset.bottom); } return restrainedSize; diff --git a/examples/ASCollectionView/Sample/ViewController.m b/examples/ASCollectionView/Sample/ViewController.m index 5e2ab4292a..f87e26f9d7 100644 --- a/examples/ASCollectionView/Sample/ViewController.m +++ b/examples/ASCollectionView/Sample/ViewController.m @@ -95,4 +95,8 @@ [context completeBatchFetching:YES]; } +- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { + return UIEdgeInsetsMake(20.0, 20.0, 20.0, 20.0); +} + @end