У меня есть пользовательский UIView
, скажем, CustomView
, который имеет UICollectionView
в качестве своего SubView. Я добавил этот customView в ячейку прототипа UITableView и прикрепил все четыре края (верхний, нижний, левый, правый) к стандартному расстоянию с помощью TableViewCell
.contentView
.
Теперь я хочу установить насекомое в разделе UICollectionView
. В этом методе - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
, когда я получаю UICollectionView.bounds.width
, я всегда получаю 600
независимо от того, запускаю ли я его на 4s, 5s, 6 или 6 plus или iPad. Это очень раздражает, и я не могу установить правильный insect
для насекомого раздела UICollectionView
. Любая помощь будет отличной. Благодарность
Вот мой код.
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
NSInteger number = [self collectionView:collectionView numberOfItemsInSection:section];
NSIndexPath *firstIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
CGSize firstSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:firstIndexPath];
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:number - 1 inSection:section];
CGSize lastSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:lastIndexPath];
UIEdgeInsets insect = UIEdgeInsetsMake(0, (collectionView.bounds.size.width - firstSize.width) / 2,
0, (collectionView.bounds.size.width - lastSize.width) / 2);
return insect;
}
вот моя инициализация моего CustomView, у которого есть collectionView как subView
- (void) initilize {
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc ] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout: flowLayout];
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.collectionView registerClass:[PickerCollectionViewCell class] forCellWithReuseIdentifier:@"PickerCollectionViewCell"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.allowsMultipleSelection = YES;
[self addSubview:self.collectionView ];
self.backgroundColor = [UIColor clearColor];
}
Приложены также два изображения для моей раскадровки.
Во втором изображении pickerView — это мой CustomView, который содержит UICollectionView
.
Любая помощь будет отличной.