调整集合视图背景和单元格间距的背景颜色和间距

时间:2014-06-27 07:57:55

标签: ios ios7 uicollectionview uicollectionviewcell uicollectionviewlayout

我有以下代码用于测试:

在viewDidLoad()

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

self.collectionView = [[UICollectionView alloc] initWithFrame:scrollView.frame collectionViewLayout:layout];


// These are required
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];



//[self.collectionView.layer addBlueGradient];

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];

[self.view addSubview:self.collectionView];


}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 50;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}

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

我创建了一个名为addBlueGradient的CALayer渐变类。如何使其成为集合视图的背景(它只是黑色或将其更改为uicolor)?如何调整单元格之间的间距?

2 个答案:

答案 0 :(得分:2)

 _collectionView.backgroundColor = [UIColor clearColor]; // for setting the collection background color 

和空间

获取剖面间距

– collectionView:layout:insetForSectionAtIndex:
– collectionView:layout:minimumLineSpacingForSectionAtIndex:
– collectionView:layout:minimumInteritemSpacingForSectionAtIndex:

答案 1 :(得分:1)

要将渐变添加到集合视图中:

CAGradientLayer *myGradient = [CAGradientLayer gradient];

// ------------------------------------------------------------------
// This assumes your collectionView bounds is not (0,0)
// when you rotate your device, you may need to update the frame
// of your gradientLayer for the orientation changes.
// ------------------------------------------------------------------
myGradientLayer.frame = CGRectMake(0, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);

myGradientLayer.colors = @[
                              (id)[UIColor whiteColor].CGColor,
                              (id)[UIColor blackColor].CGColor
                          ];

myGradientLayer.startPoint = CGPointMake(0.5, 0.0);
myGradientLayer.endPoint = CGPointMake(0.5, 1.0);

[self.collectionView.layer insertSublayer:myGradientLayer atIndex:0];

要设置单元格之间的间距,您可能需要使用许多这些委托方法:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    return UIEdgeInsetsMake(0, 9.0, 0, 9.0);

}

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