Skip to content

Commit

Permalink
[Table and collection views] Consider content inset when calculating …
Browse files Browse the repository at this point in the history
…(default) element size range (TextureGroup#525)

* Table and collection views to consider their content inset when calculating element size range

* Update CHANGELOG

* Address comments

* -[ASPagerNode currentPageIndex] to use pageSize instead of bounds

* Update documentation in ASPagerNode

* Minor change
  • Loading branch information
nguyenhuy authored and bernieperez committed Apr 25, 2018
1 parent ed9ed18 commit 0e98d2b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Remove re-entrant access to self.view when applying initial pending state. [Adlai Holler](https://github.com/Adlai-Holler) [#510](https://github.com/TextureGroup/Texture/pull/510)
- Small improvements in ASCollectionLayout [Huy Nguyen](https://github.com/nguyenhuy) [#509](https://github.com/TextureGroup/Texture/pull/509) [#513](https://github.com/TextureGroup/Texture/pull/513)
- Fix retain cycle between ASImageNode and PINAnimatedImage [Phil Larson](https://github.com/plarson) [#520](https://github.com/TextureGroup/Texture/pull/520)
- Table and collection views to consider content inset when calculating (default) element size range [Huy Nguyen](https://github.com/nguyenhuy) [#525](https://github.com/TextureGroup/Texture/pull/525)

##2.4
- Fix an issue where inserting/deleting sections could lead to inconsistent supplementary element behavior. [Adlai Holler](https://github.com/Adlai-Holler)
Expand Down
4 changes: 0 additions & 4 deletions Source/ASCollectionView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1536,10 +1536,6 @@ - (ASScrollDirection)scrollableDirections
return [self.layoutInspector scrollableDirections];
}

- (ASScrollDirection)flowLayoutScrollableDirections:(UICollectionViewFlowLayout *)flowLayout {
return (flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal) ? ASScrollDirectionHorizontalDirections : ASScrollDirectionVerticalDirections;
}

- (void)layoutSubviews
{
if (_cellsForLayoutUpdates.count > 0) {
Expand Down
5 changes: 5 additions & 0 deletions Source/ASPagerNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ NS_ASSUME_NONNULL_BEGIN

@end

/**
* A horizontal, paging collection node.
*/
@interface ASPagerNode : ASCollectionNode

/**
Expand All @@ -84,6 +87,8 @@ NS_ASSUME_NONNULL_BEGIN

/**
* Initializer with custom-configured flow layout properties.
*
* NOTE: The flow layout must have a horizontal scroll direction.
*/
- (instancetype)initWithCollectionViewLayout:(ASPagerFlowLayout *)flowLayout;

Expand Down
15 changes: 12 additions & 3 deletions Source/ASPagerNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ - (instancetype)init
- (instancetype)initWithCollectionViewLayout:(ASPagerFlowLayout *)flowLayout;
{
ASDisplayNodeAssert([flowLayout isKindOfClass:[ASPagerFlowLayout class]], @"ASPagerNode requires a flow layout.");
ASDisplayNodeAssertTrue(flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal);
self = [super initWithCollectionViewLayout:flowLayout];
return self;
}
Expand Down Expand Up @@ -112,7 +113,15 @@ - (void)didLoad

- (NSInteger)currentPageIndex
{
return (self.view.contentOffset.x / CGRectGetWidth(self.view.bounds));
return (self.view.contentOffset.x / [self pageSize].width);
}

- (CGSize)pageSize
{
UIEdgeInsets contentInset = self.view.contentInset;
CGSize pageSize = self.bounds.size;
pageSize.height -= (contentInset.top + contentInset.bottom);
return pageSize;
}

#pragma mark - Helpers
Expand Down Expand Up @@ -142,7 +151,7 @@ - (NSInteger)indexOfPageWithNode:(ASCellNode *)node
- (CGSize)sizeForElements:(ASElementMap *)elements
{
ASDisplayNodeAssertMainThread();
return self.bounds.size;
return [self pageSize];
}

#pragma mark - ASCollectionDataSource
Expand Down Expand Up @@ -179,7 +188,7 @@ - (ASSizeRange)collectionNode:(ASCollectionNode *)collectionNode constrainedSize
}
#pragma clang diagnostic pop

return ASSizeRangeMake(self.bounds.size);
return ASSizeRangeMake([self pageSize]);
}

#pragma mark - Data Source Proxy
Expand Down
3 changes: 2 additions & 1 deletion Source/ASTableView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ - (void)layoutSubviews
{
// Remeasure all rows if our row width has changed.
_remeasuringCellNodes = YES;
CGFloat constrainedWidth = self.bounds.size.width - [self sectionIndexWidth];
UIEdgeInsets contentInset = self.contentInset;
CGFloat constrainedWidth = self.bounds.size.width - [self sectionIndexWidth] - contentInset.left - contentInset.right;
if (constrainedWidth > 0 && _nodesConstrainedWidth != constrainedWidth) {
_nodesConstrainedWidth = constrainedWidth;

Expand Down
3 changes: 3 additions & 0 deletions Source/Details/ASCollectionViewLayoutInspector.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
// of the collection view
ASSizeRange NodeConstrainedSizeForScrollDirection(ASCollectionView *collectionView) {
CGSize maxSize = collectionView.bounds.size;
UIEdgeInsets contentInset = collectionView.contentInset;
if (ASScrollDirectionContainsHorizontalDirection(collectionView.scrollableDirections)) {
maxSize.width = CGFLOAT_MAX;
maxSize.height -= (contentInset.top + contentInset.bottom);
} else {
maxSize.width -= (contentInset.left + contentInset.right);
maxSize.height = CGFLOAT_MAX;
}
return ASSizeRangeMake(CGSizeZero, maxSize);
Expand Down

0 comments on commit 0e98d2b

Please sign in to comment.