viewControllerに単純なコレクションビューがあります。次のコードで構成しました。
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
layout.itemSize = CGSizeMake(372, 200);
layout.scrollDirection = UIAccessibilityScrollDirectionDown;
self.collection.collectionViewLayout = layout;
[self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
self.collection.dataSource = self;
self.collection.delegate = self;
}
2つのビューセルがあります。 cellForItemAtIndexPathのコードベースを見つけてください
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 2;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.layer.borderWidth = 1;
[cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
return cell;
}
このコードを実行すると、次のようになります。
アイテムの幅が373以上で変更された場合、このように見えます。
セル間隔はゼロでなければなりませんが、変更されていません。誰もこのバグのアイデアを持っていますか?
回答 2 件
SectionInsetはアイテムの間隔ではありません。すべてのUICollectionViewのスペースです。そのため、viewDidLoadのすべてのアイテムスペースですべてのアイテムの幅を計算します。
- (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumInteritemSpacing = 5; layout.minimumLineSpacing = 0; layout.itemSize = CGSizeMake(200, 200); CGFloat itemWidth = layout.itemSize.width + layout.minimumInteritemSpacing; CGFloat allItemWitdh = itemWidth * (int)(_collection.frame.size.width / itemWidth); CGFloat rightMargin = _collection.frame.size.width - allItemWitdh; layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, rightMargin); layout.scrollDirection = UIAccessibilityScrollDirectionDown; self.collection.collectionViewLayout = layout; [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; }
これはバグではありません。
UICollectionViewFlowLayout
のインスタンスの場合 正確な値ではなく、最小限の行と項目スペースを設定します。アイテム間のスペースを削除する必要がある場合は、sectionInset
を減らすことができますUICollectionViewFlowLayout
の 。このコードを試してください: