集合视图单元格内边距

9
我正在尝试在集合视图单元格上设置零填充,我已经在视图控制器上将“最小间隔”设置为: enter image description here 然而,它仍然在单元格之间留有间隙: enter image description here 此外,我想让单元格根据框架的宽度很好地包裹,例如每个单元格的宽度为50px,因此如果有六个单元格并且我将框架宽度设置为150px,则会显示两行三个单元格。
然而,如果我通过以下方式将框架宽度设置为150:
- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect frame = self.collectionView.frame;
    frame.size.width = 150;
    self.collectionView.frame = frame;
}

看上去屏幕截图太宽了。
如果我将其设置为非常小的值,例如 10,那么它会在一定程度上换行:
UICollectionViewCell 设置为 50 x 50:
我还尝试过通过编程设置单元格大小,并删除了 UIEdgeInset:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

我已经禁用了自动布局,以防止任何干扰。您有什么建议可以帮助我去除填充并根据框架宽度/高度进行换行?


我无法重现这个问题。我创建了一个空项目,添加了一个集合视图作为子视图,将设置与上面显示的设置相同,并在故事板中设置框架为175x175。这样可以使单元格间隔开(流布局自然完成)。我使用您的代码在viewDidLoad中更改框架大小,它变为150宽,单元格填充了间隙。还有其他未包含在您的问题中的情况。 - jrturton
啊!你正在使用UICollectionViewController。使用它,你无法更改集合视图的大小。它总是填满整个屏幕(除非你将其嵌入到容器视图控制器中)。让我看看是否可以调整插入值... - jrturton
@jrturton 感谢您花时间尝试重现此问题。虽然问题仍然存在,但我刚刚重新开始了。我在这里上传了一个示例,可能会有用:https://dl.dropbox.com/u/71160589/example.zip - StuR
4个回答

3

我不确定截图中的那些单元格大小是否为50x50(编辑:我猜它们是...)。

检查您是否连接了UICollectionViewDelegate和UICollectionViewDataSource。

这是您需要实现的方法。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize retval = CGSizeMake(50, 50);
    return retval;
}

如果不起作用,请尝试将此代码放入viewDidLoad中。
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[flowLayout setItemSize:CGSizeMake(50.0f, 50.0f)];
[self.collectionView setCollectionViewLayout:flowLayout];

这是一块4英寸的横向视网膜屏幕;尺寸看起来对我来说很合适。而您的建议只是在代码中重新实现了应该已经从nib中设置好的内容。 - jrturton
哦,是的。我没有注意到它是横向模式。尺寸似乎是正确的。然而,他第二张截图中的 Collection View 没有 150 点的宽度。至于建议的代码,我很清楚它只是重新实现了他认为已经在故事板文件中完成的工作。 - Nikola Kirev
感谢您的建议,我已经使用您的代码重新实现了它,但问题仍然存在。我已经在这里上传了一个显示该错误的示例项目:https://dl.dropbox.com/u/71160589/example.zip - StuR

3
在你的ViewController中实现这个方法。
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
   return 2; //the spacing between cells is 2 px here
}

3
你正在使用一个UICollectionViewController,就像UITableViewController一样,该控制器把集合视图(或表格视图)作为其基本view属性的视图控制器。这意味着它不能从视图控制器内部调整大小;它的大小由其父视图控制 - 在这种情况下是窗口,它将其作为根视图控制器,或者如果您嵌入它,则为父视图控制器。

如果您想减少集合视图区域以使单元格相互邻接,可以在storyboard中修改集合视图的section insets。在您的情况下,左右各15个插图将单元格放在一起 - 这是9 * 50(450)加30 = 480,这是3.5英寸iPhone在横向上的宽度。

显然,在iPhone 5或iPad上会有所不同。您可以在运行时计算插图、使用标准的UIViewController子类中持有集合视图,或者将UICollectionViewController保持为嵌入式视图控制器。后两种方法将使您只需指定大小,这可能比计算插图更好。


非常感谢,我已经快要完成了。我在普通的UIViewController子类中添加了一个UICollectionView,但是我仍然无法调整它的大小:https://dl.dropbox.com/u/71160589/example2.zip - StuR
我暂时不能查看这个,但你要么没有连接插座,要么启用了自动布局并仍在设置框架而不是编辑约束。 - jrturton
我在UIViewController中使用了UICollectionView - 这样做不允许我调整UICollectionView的大小。然而,我添加了另一个视图,现在层次结构看起来像是UIViewController -> UIView -> UICollectionView。然后我调整了UIView的大小,它奏效了。终于 :) 感谢你的帮助。 - StuR

0

minimumInteritemSpacing只是一个最小值,如果UICollectionViewLayout类决定需要更大的间距,则可以更大。

为了纠正这个问题,您可以创建自己的布局子类,并实现- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath方法,类似于this question的答案(请参见第二个问题,而不是标记为正确的问题)。

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes* atts =
    [super layoutAttributesForItemAtIndexPath:indexPath];

    if (indexPath.item == 0) // degenerate case 1, first item of section
        return atts;

    NSIndexPath* ipPrev =
    [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];

    CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
    CGFloat rightPrev = fPrev.origin.x + fPrev.size.width + 10;
    if (atts.frame.origin.x <= rightPrev) // degenerate case 2, first item of line
        return atts;

    CGRect f = atts.frame;
    f.origin.x = rightPrev;
    atts.frame = f;
    return atts;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接