【发布时间】:2014-01-12 03:40:16
【问题描述】:
目标
我正在尝试减少 UICollectionViewCells 之间的空间
我尝试了什么
我尝试继承 UICollectionViewFlowLayout 并覆盖此方法:
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for(int i = 1; i < [answer count]; ++i) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
NSInteger maximumSpacing = 4;
NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return answer;
}
然后在我的 UICollectionViewController viewDidLoad:
SublassedcollectionViewLayout *space = [[SublassedcollectionViewLayout alloc]init];
self.collectionView.collectionViewLayout = space;
问题
这只会让我的 UICollectionViewCells 更小。
编辑:我想减少从左到右而不是从上到下的间距。
任何帮助将不胜感激!
【问题讨论】:
-
@skladek 我不想在单元格上设置边距。我正在尝试减少一个单元格与另一个单元格之间的空间。
-
这将通过增加单元格的大小或在左右添加边距来实现。集合视图的工作原理是取全宽,从每一侧减去边距,然后用剩余空间均匀地间隔元素。如果您希望单元格靠得更近,则需要减少剩余空间。
标签: ios uicollectionviewcell spacing uicollectionviewlayout