iOS - UICollectionView 间距设置为 0 时仍然存在 - 如何设置单元格之间没有间距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19250284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - UICollectionView spacing still there when set to 0 - How to set with no spacing between cells
提问by Zigglzworth
I have a simple UICollectionView which I have set with 0 spacing in InterfaceBuilder but when I populate the collection view with cells there is still some spacing. Is there something special and not immediately obvious that I need to do in order to actually see a collectionview cell with 0 spacing beyond setting it to have 0 spacing? Thanks.
我有一个简单的 UICollectionView,我在 InterfaceBuilder 中设置了 0 间距,但是当我用单元格填充集合视图时,仍然有一些间距。是否有一些特别的并且不是很明显的事情,我需要做些什么才能实际看到一个间距为 0 的 collectionview 单元格,而不是将其设置为 0 间距?谢谢。
EDIT* some code:
编辑*一些代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, cell.frame.size.width -4, cell.frame.size.height -4)];
lbl.backgroundColor = [UIColor clearColor];
lbl.font = [UIFont boldSystemFontOfSize:20];
lbl.text = [NSString stringWithFormat:@"$%0.0f", [[amountsArray objectAtIndex:indexPath.row] floatValue]];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.layer.borderWidth = 1;
[cell addSubview:lbl];
[lbl release];
return cell;
}
采纳答案by Zigglzworth
I solved this issue and got the layout I desired with the following:
我解决了这个问题,并通过以下方式获得了我想要的布局:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
//clear any contents on the cell
for (UIView *subView in [cell subviews]) {
[subView removeFromSuperview];
}
//Label to put on the cell
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, cell.frame.size.width -4, cell.frame.size.height -4)];
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor colorWithRed:[CPExtras RGBtoPercent:70] green:[CPExtras RGBtoPercent:92] blue:[CPExtras RGBtoPercent:105] alpha:1];
lbl.font = [UIFont boldSystemFontOfSize:20];
lbl.text = @"100";
lbl.textAlignment = NSTextAlignmentCenter;
//Give the cell a border
cell.layer.borderColor = [[UIColor colorWithRed:[CPExtras RGBtoPercent:70] green:[CPExtras RGBtoPercent:92] blue:[CPExtras RGBtoPercent:105] alpha:0.5] CGColor];
cell.layer.borderWidth = 0.5;
[cell addSubview:lbl];
[lbl release];
return cell;
}
In IB I had these measurement settings for the collectionview:
在 IB 中,我为 collectionview 设置了这些测量设置:
回答by Mihir Oza
Simple solution for your Query. Add this in your viewController's .m file:
查询的简单解决方案。在 viewController 的 .m 文件中添加:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
ProductDetailViewController *HomeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ProductDetailView"];
HomeVC.title = @"DemoProject";
[self.navigationController pushViewController:HomeVC animated:YES];
}
- (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 0.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0.0;
}
回答by Vojtech Vrbka
You have to create custom UICollectionViewLayout
.
您必须创建自定义UICollectionViewLayout
.
Space between the cells will be equal to cellSpacing
.
单元格之间的空间将等于cellSpacing
。
final class CustomFlowLayout: UICollectionViewFlowLayout {
let cellSpacing: CGFloat = 0
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
if let attributes = super.layoutAttributesForElements(in: rect) {
for (index, attribute) in attributes.enumerated() {
if index == 0 { continue }
let prevLayoutAttributes = attributes[index - 1]
let origin = prevLayoutAttributes.frame.maxX
if (origin + cellSpacing + attribute.frame.size.width < self.collectionViewContentSize.width) {
attribute.frame.origin.x = origin + cellSpacing
}
}
return attributes
}
return nil
}
}
回答by MBH
Swift 3version of @MihirOza's solution
@MihirOza解决方案的Swift 3版本
Worked for both Horizontal and Vertical collection views
适用于水平和垂直集合视图
Code
代码
// removing spacing
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
回答by David Blue
In order to actually have zero space, the number of cells and their width should be divisible by the collection view's own width, for example if you have 5 cells at a time with a width of 100px, then your collection view should have 500px in width, if it's larger then it will force a space between cells.
为了实际拥有零空间,单元格的数量及其宽度应该可以被集合视图自己的宽度整除,例如,如果您一次有 5 个单元格,宽度为 100 像素,那么您的集合视图的宽度应该为 500 像素,如果它更大,那么它会强制单元格之间有一个空间。
回答by Pascal Bourque
The documentationfor [UICollectionViewFlowLayout minimumInteritemSpacing]
mentions:
该文档的[UICollectionViewFlowLayout minimumInteritemSpacing]
提到:
This spacing is used to compute how many items can fit in a single line, but after the number of items is determined, the actual spacing may possibly be adjusted upward.
这个间距是用来计算一行可以容纳多少个item,但是在确定item的个数之后,实际的间距可能会向上调整。
You may need to implement a custom layout to do this. The documentation can be found here, and an example here.