如何删除 UICollectionViewCells 之间的 space?
How can I remove the space between UICollectionViewCells?
我试图删除 UICollectionView
之间的 space,这是设备
的屏幕截图
我真的想避免那些 spaces(蓝色)并且我尝试了以下代码
这是我的 collectionview 委托
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
我首先尝试了这个,但它不起作用
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cell.frame.size.width, cell.frame.size.height);
flow.scrollDirection = UICollectionViewScrollPositionCenteredVertically;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
}
我也试了这个,没用
#pragma mark collection view cell paddings
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 5.0;
}
请帮我解决这个问题。
flow.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
在 viewdidload
中为你的 UICollectionViewFlowLayout
使用 sectionInset
。
所以有属性和方法可以限制最小space,比如minimumInteritemSpacingForSectionAtIndex
然而,它只是 'minimum space' 并不总是最大值 space。
这是我正在使用的:借用Cell spacing in UICollectionView
覆盖标准流程布局:
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [super layoutAttributesForElementsInRect:rect];
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;
}
maximumSpacing 可以设置为您喜欢的任何值
您还可以从情节提要中设置间距属性。 Select 您的 collection 视图 - select 大小检查器选项卡,您可以在其中调整 单元格 的 最小间距 ] 或 行。
您还可以调整部分插入。
请参阅 my answer 了解如何调整 UICollectionView 宽度(使用 AutoLayout 约束)以完美适合所有单元格且它们之间没有任何间距。
我试图删除 UICollectionView
之间的 space,这是设备
我真的想避免那些 spaces(蓝色)并且我尝试了以下代码
这是我的 collectionview 委托
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
我首先尝试了这个,但它不起作用
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cell.frame.size.width, cell.frame.size.height);
flow.scrollDirection = UICollectionViewScrollPositionCenteredVertically;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
}
我也试了这个,没用
#pragma mark collection view cell paddings
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 5.0;
}
请帮我解决这个问题。
flow.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
在 viewdidload
中为你的 UICollectionViewFlowLayout
使用 sectionInset
。
所以有属性和方法可以限制最小space,比如minimumInteritemSpacingForSectionAtIndex
然而,它只是 'minimum space' 并不总是最大值 space。
这是我正在使用的:借用Cell spacing in UICollectionView
覆盖标准流程布局:
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [super layoutAttributesForElementsInRect:rect];
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;
}
maximumSpacing 可以设置为您喜欢的任何值
您还可以从情节提要中设置间距属性。 Select 您的 collection 视图 - select 大小检查器选项卡,您可以在其中调整 单元格 的 最小间距 ] 或 行。
您还可以调整部分插入。
请参阅 my answer 了解如何调整 UICollectionView 宽度(使用 AutoLayout 约束)以完美适合所有单元格且它们之间没有任何间距。