【问题标题】:Adjusting background color and spacing of a collection view background and cell spacing调整集合视图背景和单元格间距的背景颜色和间距
【发布时间】:2014-08-18 06:23:31
【问题描述】:
我有以下测试代码:
在 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)?如何调整单元格间距?
【问题讨论】:
标签:
ios
ios7
uicollectionview
uicollectionviewcell
uicollectionviewlayout
【解决方案1】:
_collectionView.backgroundColor = [UIColor clearColor]; // for setting the collection background color
为了空间
获取截面间距
– collectionView:layout:insetForSectionAtIndex:
– collectionView:layout:minimumLineSpacingForSectionAtIndex:
– collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
【解决方案2】:
要将渐变添加到集合视图中:
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;
}