向 UICollectionView 的第一个和最后一个单元格添加填充
Adding padding to first and last cell of UICollectionView
我已经将 UICollectionViewFlowLayout 子类化以获得具有类似分页行为的水平 UICollectionView。只要 UICollectionViewCell 不是最后一个单元格的第一个,它就可以正常工作。下面附上图片。
除了跟随之外,我是否需要在我的 UICollectionViewFlowLayout 中覆盖某些内容?
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offSetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = (CGFloat) (proposedContentOffset.x + (self.collectionView.bounds.size.width / 2.0));
CGRect targetRect = CGRectMake(proposedContentOffset.x,
0.0,
self.collectionView.bounds.size.width,
self.collectionView.bounds.size.height);
NSArray *array = [self layoutAttributesForElementsInRect:targetRect];
for (UICollectionViewLayoutAttributes *layoutAttributes in array)
{
if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell)
{
CGFloat itemHorizontalCenter = layoutAttributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offSetAdjustment))
{
offSetAdjustment = itemHorizontalCenter - horizontalCenter;
}
}
}
CGFloat nextOffset = proposedContentOffset.x + offSetAdjustment;
do {
proposedContentOffset.x = nextOffset;
CGFloat deltaX = proposedContentOffset.x - self.collectionView.contentOffset.x;
CGFloat velX = velocity.x;
if(deltaX == 0.0 || velX == 0 || (velX > 0.0 && deltaX > 0.0) || (velX < 0.0 && deltaX < 0.0))
{
break;
}
if(velocity.x > 0.0)
{
nextOffset += [self snapStep];
}
else if(velocity.x < 0.0)
{
nextOffset -= [self snapStep];
}
} while ([self isValidOffset:nextOffset]);
proposedContentOffset.y = 0.0;
return proposedContentOffset;
}
- (BOOL)isValidOffset:(CGFloat)offset
{
return (offset >= [self minContentOffset] && offset <= [self maxContentOffset]);
}
- (CGFloat)minContentOffset
{
return -self.collectionView.contentInset.left;
}
- (CGFloat)maxContentOffset
{
return [self minContentOffset] + self.collectionView.contentSize.width - self.itemSize.width;
}
- (CGFloat)snapStep
{
return self.itemSize.width + self.minimumLineSpacing;
}
任何指示/评论都会有用。
在设置集合视图的框架时,您可以将 space 设置为左右边距等于您的内边距。
或
您可以在 cellForItemAtIndexPath
中设置条件,如果它是第一个单元格或最后一个单元格,则相应地管理填充。就是这样。
或
您可以设置您的 collectionView contentInset
属性。
例如,
UICollectionView *cv; // your collectionView
cv.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
或者,您可以在故事板中设置 UICollectionView
contentInset
以使其正常工作。
很简单,您可以使用 collectionview 方法来设置 UIEdgeInsets,就像下面的方法一样。
-(UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0,10,0,10); // top, left, bottom, right
}
在这里您可以为第一个和最后一个单元格传递左侧 space 和右侧 space 的值,您还可以通过波纹管方法在两个单元格之间提供最小值 space
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 5.0;
}
您可以通过更改 Interface Builder 中的插图来实现。它们可以作为 Section Insets 在 Size Inspector:
中找到
对于Swift 5:
在您的 viewDidLoad
中,像这样设置 collectionView
的 contentInset
属性:
self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);
已接受的解决方案有效,但如果您启用了 pagingEnabled,集合视图分页将被破坏。
对我来说,解决方案是使用:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
}
您可以使用这段代码进行填充
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 60, left: 0, bottom: 40, right: 0)
}
我已经将 UICollectionViewFlowLayout 子类化以获得具有类似分页行为的水平 UICollectionView。只要 UICollectionViewCell 不是最后一个单元格的第一个,它就可以正常工作。下面附上图片。
除了跟随之外,我是否需要在我的 UICollectionViewFlowLayout 中覆盖某些内容?
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offSetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = (CGFloat) (proposedContentOffset.x + (self.collectionView.bounds.size.width / 2.0));
CGRect targetRect = CGRectMake(proposedContentOffset.x,
0.0,
self.collectionView.bounds.size.width,
self.collectionView.bounds.size.height);
NSArray *array = [self layoutAttributesForElementsInRect:targetRect];
for (UICollectionViewLayoutAttributes *layoutAttributes in array)
{
if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell)
{
CGFloat itemHorizontalCenter = layoutAttributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offSetAdjustment))
{
offSetAdjustment = itemHorizontalCenter - horizontalCenter;
}
}
}
CGFloat nextOffset = proposedContentOffset.x + offSetAdjustment;
do {
proposedContentOffset.x = nextOffset;
CGFloat deltaX = proposedContentOffset.x - self.collectionView.contentOffset.x;
CGFloat velX = velocity.x;
if(deltaX == 0.0 || velX == 0 || (velX > 0.0 && deltaX > 0.0) || (velX < 0.0 && deltaX < 0.0))
{
break;
}
if(velocity.x > 0.0)
{
nextOffset += [self snapStep];
}
else if(velocity.x < 0.0)
{
nextOffset -= [self snapStep];
}
} while ([self isValidOffset:nextOffset]);
proposedContentOffset.y = 0.0;
return proposedContentOffset;
}
- (BOOL)isValidOffset:(CGFloat)offset
{
return (offset >= [self minContentOffset] && offset <= [self maxContentOffset]);
}
- (CGFloat)minContentOffset
{
return -self.collectionView.contentInset.left;
}
- (CGFloat)maxContentOffset
{
return [self minContentOffset] + self.collectionView.contentSize.width - self.itemSize.width;
}
- (CGFloat)snapStep
{
return self.itemSize.width + self.minimumLineSpacing;
}
任何指示/评论都会有用。
在设置集合视图的框架时,您可以将 space 设置为左右边距等于您的内边距。
或
您可以在 cellForItemAtIndexPath
中设置条件,如果它是第一个单元格或最后一个单元格,则相应地管理填充。就是这样。
或
您可以设置您的 collectionView contentInset
属性。
例如,
UICollectionView *cv; // your collectionView
cv.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
或者,您可以在故事板中设置 UICollectionView
contentInset
以使其正常工作。
很简单,您可以使用 collectionview 方法来设置 UIEdgeInsets,就像下面的方法一样。
-(UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0,10,0,10); // top, left, bottom, right
}
在这里您可以为第一个和最后一个单元格传递左侧 space 和右侧 space 的值,您还可以通过波纹管方法在两个单元格之间提供最小值 space
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 5.0;
}
您可以通过更改 Interface Builder 中的插图来实现。它们可以作为 Section Insets 在 Size Inspector:
中找到对于Swift 5:
在您的 viewDidLoad
中,像这样设置 collectionView
的 contentInset
属性:
self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);
已接受的解决方案有效,但如果您启用了 pagingEnabled,集合视图分页将被破坏。
对我来说,解决方案是使用:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
}
您可以使用这段代码进行填充
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 60, left: 0, bottom: 40, right: 0)
}