insetForSectionAtIndexを実装。
// これは横スクロールだけを考えたCollectionViewで使用
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
NSInteger count = item数;
CGFloat totalCellWidth = Cell幅 * count;
CGFloat totalSpaceWidth = Cell間のスペース * (count - 1);
CGFloat inset = (collectionView.layer.frame.size.width - (totalCellWidth + totalSpaceWidth)) / 2;
if (inset > 0) {
return UIEdgeInsetsMake(0, inset, 0, inset);
}else{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
}
左右のinsetがマイナスになると、画面幅を超えたサイズが生成されて、横スクロールしなくなるので、マイナスの時は画面幅としてあげるためのinsetゼロです。