Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Table and collection views] Consider content inset when calculating (default) element size range #525

Merged
merged 6 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method isn't used anywhere.

- (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);
Copy link
Member Author

@nguyenhuy nguyenhuy Aug 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing implementation of this class (e.g -currentPageIndex and - scrollToPageAtIndex: animated:) assumes a horizontal scroll direction. Let's formalize that.

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old implementation works as expected, but let's not assume that self.view.bounds.width == 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