#ios #ios7 #uicollectionview #uicollectionviewcell #uicollectionviewlayout
#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);
}
Я создал категорию градиента CALayer под названием addBlueGradient. Как мне сделать это фоном представления коллекции (оно просто черное или изменить его на uicolor)? Как мне настроить интервал между ячейками?
Ответ №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;
}