iOS - 设置为0时仍然存在UICollectionView间距 - 如何设置单元格之间没有间距 [英] iOS - UICollectionView spacing still there when set to 0 - How to set with no spacing between cells

查看:603
本文介绍了iOS - 设置为0时仍然存在UICollectionView间距 - 如何设置单元格之间没有间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的UICollectionView,我在InterfaceBuilder中设置了0间距但是当我用单元格填充集合视图时,仍然有一些间距。有什么特别的东西,并不是很明显,我需要做的是为了实际看到一个带有0间距的集合视图单元格,除了设置它有0间距?谢谢。

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.

编辑*一些代码:

  - (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;
}

推荐答案

我解决了这个问题并得到了我想要的布局以下内容:

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;
}

在IB中我收集了这些测量设置:

In IB I had these measurement settings for the collectionview:

这篇关于iOS - 设置为0时仍然存在UICollectionView间距 - 如何设置单元格之间没有间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆