Eve*_*rus 8 ios uicollectionview uicollectionviewlayout
我在水平滚动上有不同高度的单元格UICollectionView,看起来是垂直均匀地分布单元格,在单元格之间留下空白空间,我想顶部对齐它们,并在每列的底部有可变的空白空间.
Eve*_*rus 13
我伸出我的UICollectionViewFlowLayout提供给我的UICollectionView.以下压倒一切对我有用.
#import "TopAlignedCollectionViewFlowLayout.h"
const NSInteger kVerticalSpacing = 10;
@implementation TopAlignedCollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) {
if (nil == attributes.representedElementKind) {
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* currentItemAttributes =
[super layoutAttributesForItemAtIndexPath:indexPath];
UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout sectionInset];
if (indexPath.item == 0) {
CGRect frame = currentItemAttributes.frame;
frame.origin.y = sectionInset.top;
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
CGFloat previousFrameRightPoint = previousFrame.origin.y + previousFrame.size.height + kVerticalSpacing;
CGRect currentFrame = currentItemAttributes.frame;
CGRect strecthedCurrentFrame = CGRectMake(currentFrame.origin.x,
0,
currentFrame.size.width,
self.collectionView.frame.size.height
);
if (!CGRectIntersectsRect(previousFrame, strecthedCurrentFrame)) {
CGRect frame = currentItemAttributes.frame;
frame.origin.y = frame.origin.y = sectionInset.top;
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
CGRect frame = currentItemAttributes.frame;
frame.origin.y = previousFrameRightPoint;
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
@end
Run Code Online (Sandbox Code Playgroud)
我想您正在使用 UICollectionViewFlowLayout 来布局单元格。我不认为你可以通过流程布局来做到这一点。您可能必须编写 UICollectionViewLayout 的自定义子类来对齐每个单元格的顶部边缘。
这不是一个非常漂亮的解决方案,但是强制所有单元格具有相同的大小并在单元格中设置约束以将内容与顶部对齐怎么样?这可以模拟您想要的外观,而无需子类化 UICollectionViewLayout。
| 归档时间: |
|
| 查看次数: |
8145 次 |
| 最近记录: |