如何在iOS中的uicollectionview单元格之间给出相等的间距?

时间:2015-10-06 14:30:44

标签: ios objective-c uicollectionview

我在我的应用中添加了UICollectionView。它工作正常,但问题是细胞之间的弹簧太多了。我想在细胞之间给出相等的间距。在iPad设备上,我希望空间更加紧张。请告诉我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个:

// Configure layout
        UICollectionViewFlowLayout *flowLayout_D = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout_D setItemSize:CGSizeMake(102, 96)]; //your cell size
        [flowLayout_D setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        [self->mycollectionView setCollectionViewLayout:flowLayout_D];

并且您有委托方法来更改间距:

#pragma mark Collection view layout things

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (collectionView==myCollectionView) {
    return 2.0;
    }
    else
        return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (collectionView==myCollectionView) {
        return 2.0;
    }
    else
        return 0;
}

您还可以更改边缘插图:

// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
    if (collectionView==myCollectionView) {
    return UIEdgeInsetsMake(0,0,0,0);
    }// top, left, bottom, right
    else
        return  UIEdgeInsetsMake(0,0,0,0);

}